|
I have a base class:
class base;
And some derived classes from base:
class derived1 : public base {
double a;
int b;
}; // << should output both a and b
class derived2 : public base {
string c;
}; // << should output only c
How could I implement a << operator overloading that can work on
both derived1 and derived2? What's more, it can determine the type
that pointed to by a base pointer, like,
base *pbase;
derived1 d1;
derived2 d2;
cout<<d1;
cout<<d2;
pbase = &d1;
cout<<*pbase; // should give output the same as d1
pbase = &d2;
cout<<*pbase; // should give output the same as d2
How can I implement such an operator overloading? virtual seems won't
work with << overloading. |
|