This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Abstract class


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.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]