LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 481|回复: 0

<< operator overloading

[复制链接]
发表于 2004-4-11 05:36:20 | 显示全部楼层 |阅读模式
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.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表