Suppose we have a following piece of code:
class Base {
public:
int a = 5;
};
class Derived : public Base {
public:
Base *parent_ = new Base;
Base* parent() const { return parent_; }
};
void f(const Derived *derived) {
Base *p = derived->parent();
p->a = 10; // <- is this correct?
}
Personally I think here is a problem:
In function
f
we take a pointer toconst
object of classDerived
. This makes each member of it alsoconst
thusparent_
becomesconst Base *
. If it is const we should not have an ability to modify the object on which the pointer points.
Where am I wrong?