Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Access derived class member from base class pointer

If I allocate an object of a class Derived (with a base class of Base), and store a pointer to that object in a variable that points to the base class, how can I access the members of the Derived class?

Here's an example:

class Base {     public:     int base_int; };  class Derived : public Base {     public:     int derived_int; };  Base* basepointer = new Derived(); basepointer-> //Access derived_int here, is it possible? If so, then how? 
like image 240
Peter Avatar asked Mar 12 '10 21:03

Peter


People also ask

Can you access the public members of the derived class through a pointer of the base class?

Explanation: A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer because object slicing happens when a derived class object is assigned to a base class object.

Can base class access derived members?

A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

How do you access base class members in a derived class?

You can use both a structure and a class as base classes in the base list of a derived class declaration: If the derived class is declared with the keyword class , the default access specifier in its base list specifiers is private .

Can a base class pointer call methods in the derived class?

When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.


2 Answers

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 
like image 183
Peter Alexander Avatar answered Sep 21 '22 14:09

Peter Alexander


You're dancing on a minefield here. The base class can never know that it's actually an instance of the derived. The safest way to do that would be to introduce a virtual function in the base:

class Base  {  protected:  virtual int &GetInt()  {   //Die horribly  }  public:   int base_int;  };   class Derived : Base  {    int &GetInt()   {     return derived_int;   } public:  int derived_int  };   basepointer->GetInt() = 0; 

If basepointer points as something other that a Derived, your program will die horribly, which is the intended result.

Alternatively, you can use dynamic_cast<Derived>(basepointer). But you need at least one virtual function in the Base for that, and be prepared to encounter a zero.

The static_cast<>, like some suggest, is a sure way to shoot yourself in the foot. Don't contribute to the vast cache of "unsafety of the C language family" horror stories.

like image 34
Seva Alekseyev Avatar answered Sep 23 '22 14:09

Seva Alekseyev