class Parent
{
...
friend ostream& operator<<(ostream&, const Parent&);
};
class Child : public Parent
{
...
friend ostream& operator<<(ostream&, const Child&);
};
ostream& operator<< (ostream& os, const Parent& p)
{
os << ... ;
return os;
}
ostream& operator<< (ostream& os, const Child& c)
{
os << c.Parent << ... ; // can't I access the subobject on this way?
return os;
}
How do I call the operator of Parent inside of the operator of Child? That just gives me the error "invalid use of Parent::Parent"
c.Parent
is not a valid syntax, neither is your operator<<
a member function. To call the proper overload, change the context of c
:
ostream& operator<<(ostream& os, const Child& c)
{
os << static_cast<const Parent&>(c);
return os;
}