Abstract class
Rupert Wood
me@rupey.net
Wed Dec 24 10:52:00 GMT 2008
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.
More information about the Gcc-help
mailing list