No, you cannot access derived_int
because derived_int
is part of Derived
, while basepointer
is a pointer to Base
.
You can do it the other way round though:
Derived* derivedpointer = new Derived;
derivedpointer->base_int; // You can access this just fine
Derived classes inherit the members of the base class, not the other way around.
However, if your basepointer
was pointing to an instance of Derived
then you could access it through a cast:
Base* basepointer = new Derived;
static_cast<Derived*>(basepointer)->derived_int; // Can now access, because we have a derived pointer
Note that you'll need to change your inheritance to public
first:
class Derived : public Base
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…