Since A is a constant pointer, you can only call const
methods on it. There are two possible solutions:
- if you need to call a non-const method on A: remove the
const
specifier fromvoid A::foo () const
, since the function is actually modifyingthis
through the calls to B. - if you don't need to call a non-const method on A: make
anotherMethodOfA
and any other method invoked on A inside Bconst
too.
The error you get is legitimate, otherwise you would be violating the definition of pure method.
If you need foo
to be const
and the methods invoked on A inside foo
do not change it in a way that is visible through the public interface (e.g. perform some caching or the like), you could also try using the mutable
specifier with modified fields. But don't abuse this feature, please!