Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store derived class objects in base class variables

I would like to store instances of several classes in a vector. Since all classes inherit from the same base class this should be possible.

Imagine this program:

#include <iostream> #include <vector> using namespace std;  class Base {     public:     virtual void identify ()     {         cout << "BASE" << endl;     } };  class Derived: public Base {     public:     virtual void identify ()     {         cout << "DERIVED" << endl;     } };  int main () {     Derived derived;      vector<Base> vect;     vect.push_back(derived);      vect[0].identify();     return 0; } 

I expected it to print "DERIVED", because the "identify" method is virtual. Instead 'vect[0]' seems to be a 'Base' instance and it prints

BASE

I guess I could write my own container (probably derived from vector) somehow that is capable of doing this (maybe holding only pointers...). I just wanted to ask if there is a more C++ish method for doing this. AND I would like to be completely vector-compatible (just for convenience if other users should ever use my code).

like image 565
drakide Avatar asked Jan 08 '12 13:01

drakide


People also ask

Can derived class objects be assigned to base class?

Object Slicing in C++ In C++, a derived class object can be assigned to a base class object, but the other way is not possible.

Is base class pointer store the derived class object address?

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 a derived class pointer point to a base class object?

Derived class pointer cannot point to base class.

Can be inherited by a derived class from a base class?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.


1 Answers

What you are seeing is Object Slicing.
You are storing object of Derived class in an vector which is supposed to store objects of Base class, this leads to Object slicing and the derived class specific members of the object being stored get sliced off, thus the object stored in the vector just acts as object of Base class.

Solution:

You should store pointer to object of Base class in the vector:

vector<Base*>  

By storing a pointer to Base class there would be no slicing and you can achieve the desired polymorphic behavior as well.
Since you ask for a C++ish way of doing this, the right approach is to use a suitable Smart pointer instead of storing a raw pointer in the vector. That will ensure you do not have to manually manage the memory, RAII will do that for you automatically.

like image 154
Alok Save Avatar answered Sep 21 '22 14:09

Alok Save