This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: Abstract class
- From: "Rupert Wood" <me at rupey dot net>
- To: "'Kolesar'" <asim dot husanovic at gmail dot com>
- Cc: <gcc-help at gcc dot gnu dot org>
- Date: Wed, 24 Dec 2008 10:09:10 -0000
- Subject: RE: Abstract class
- References: <21156603.post@talk.nabble.com>
Kolesar wrote:
> tmp->dispatch(session_cntx_impl(5));
The problem is that the temporary session_cntx_impl created here is implicitly const whereas dispatch expects a non-const reference. One way to fix this is to create a non-const session_cntx_impl, e.g.
session_cntx_impl ctximp(5);
tmp->dispatch(ctximp);
or, as you tried, make the dispatch function accept const references. The error you got then
> error: passing âconst context_fncâ as âthisâ argument of âvirtual
> void context_fnc::exec()â discards qualifiers
means you need to keep adding const; you have a const context_fnc object but you're trying to call a non-const method, exec(), on it. If you make exec() a const method throughout your code i.e.
virtual void exec() const = 0;
(and in the subclass and in the method declaration) then this should compile correctly.
Whether you want to add the consts or create the non-const session_cntx_impl depends on what your exec() methods do and whether they can always work on const objects.
Rup.