I have the following problem.
I've created an array of pointers to objects from the base class, but I'm storing in this array also the pointers to the objects from the derived classes.
I also overloaded the <<operator
in each class to display the objects.
However, when I apply this overloaded <<operator
to the above mentioned array, it treats all the pointers as if there were pointing to the objects of the base class.
Below I present the code that depicts the problem. I need this overloaded operator to work correctly because I need to save the objects pointed by the array in the file.
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
class Base
{
public:
int basevar;
Base(): basevar(1) {};
virtual void dosth(){};
friend ostream & operator<<(ostream & screen, const Base & obj);
};
ostream & operator<<(ostream & screen, const Base & obj)
{
screen << obj.basevar;
return screen;
};
class Der1: public Base
{
public:
int de1;
Der1(): de1(2) {};
virtual void dosth()
{
cout << "Der1" << endl;
}
friend ostream & operator<<(ostream & screen, const Der1 & obj);
};
ostream & operator<<(ostream & screen, const Der1 & obj)
{
Base b;
b = static_cast <Base>(obj);
screen << b;
screen << " " << obj.de1;
return screen;
};
class Der2: public Base
{
public:
int de2;
Der2(): de2(3) {};
virtual void dosth()
{
cout << "Der2" << endl;
}
friend ostream & operator<<(ostream & screen, const Der2 & obj);
};
ostream & operator<<(ostream & screen, const Der2 & obj)
{
Base b;
b = static_cast <Base>(obj);
screen << b;
screen << " " << obj.de2;
return screen;
}
int main()
{
Base * array[] = {new Base(), new Der1(), new Der2()};
for(int i=0; i<3; ++i)
{
cout << *array[i]; // <- always displays objects as if they were from the base class
}
return 0;
}