I’ve got 2 classes, base class is “Port” and derived class is “VintagePort”.
As far as I know If i use reference or pointer of base class to object of derived class, it automatically finds correct method, not for reference or pointer but exactly to object(if methods are virtual).
In my situation you can see both classes have friend function “operator<<“. But it looks like when I’m using pointer for base class, it calls function only from base class. If I use “cout << VintagePort” It works ok.
My question: Is it working correctly or I should fix something in code?
std::ostream& operator<<(std::ostream& os, const Port& p)
{
os << p.brand << ", " << p.style << ", " << p.bottles << endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const VintagePort& vp)
{
os << (const Port &) vp;
cout << ", " << vp.nickname << ", " << vp.year << endl;
return os;
}
VintagePort vp1;
VintagePort vp2("Gallo", "lekko brazowy", 50, "Blaze", 1990);
VintagePort vp3(vp2);
Port* arr[3];
arr[0] = &vp1;
arr[1] = &vp2;
arr[2] = &vp3;
for (int i = 0; i < 3; i++)
{
cout << ">>>>> " << i+1 << " <<<<<" << endl;
cout << *arr[i]; // call for base class instead derived class
arr[i]->Show();
}
The compiler doesn’t now the pointer actually points to an inherited class. One way to solve this is to have a virtual function in the base class for output, and override it in the class inheriting the base class. Then call this virtual method in the output operator.