On 11/4/2010 8:10 AM, Andrea Crotti wrote:
> "Daniel T."<> writes:
>
>>
>> What's wrong with doing this?
>>
>> class Base {
>> friend ostream& operator<<(ostream& s, const Base& c);
>> };
>>
>> class Extended : public Base { };
>>
>> ostream& operator<<(ostream& s, const Base& b) {
>> s<< "base class"<< endl;
>> return s;
>> }
>>
>> int main() {
>> Extended e;
>> cout<< e<< '\n';
>> }
>>
>> The above compiles fine and does what you wanted.
>
>
> and what if you want instead want to print
> "extended class base class"
>
> So first calling the specialized method and from it the base class?
>
> How do I tell to use Base:
perator<< in that case?
You need to introduce polymorphism and call a virtual function in the
class Base. Something like
class Base {
...
virtual ostream& print(ostream& os) const
{
return os << "base class";
}
};
class Extended : public Base {
ostream& print(ostream& os) const
{
os << "extended class ";
return Base:

rint(os);
}
};
ostream& operator << (ostream& os, const Base& c)
{
return c.print(os);
}
V
--
I do not respond to top-posted replies, please don't ask