插件/提取器操作符覆盖是否可以运行时多态? [英] Can inserter/extractor operator overrides be made run-time polymorphic?
问题描述
由于这些操作符不能成为成员函数,并且由于朋友函数不能被声明为虚拟,所以我如何制作我的
插入器和提取器多态的?
--Randy Yates
Since these operators can''t be member functions, and since
friend functions can''t be declared virtual, how do I make my
inserters and extractors polymorphic?
--Randy Yates
推荐答案
Randy写道:
由于这些运算符不能成为成员函数,并且由于朋友函数不能被声明为虚拟,我如何使我的
插入器和提取器变为多态?
Since these operators can''t be member functions, and since
friend functions can''t be declared virtual, how do I make my
inserters and extractors polymorphic?
将另一个成员虚拟化并在运营商中调用它<<。
struct Base {
虚拟ostream的和放;输出(ostream&)const = 0;
};
ostream&运算符<< (ostream& os,Base const& b){
返回b.output(os);
}
struct Derived :Base {...
V
Make another member virtual and call it in your operator <<.
struct Base {
virtual ostream& output(ostream&) const = 0;
};
ostream& operator << (ostream& os, Base const& b) {
return b.output(os);
}
struct Derived : Base { ...
V
2006年1月12日11:25:27 -0800,Randy <亚*** @ ieee.org>写道:
On 12 Jan 2006 11:25:27 -0800, "Randy" <ya***@ieee.org> wrote:
由于这些运算符不能成为成员函数,并且由于朋友函数不能被声明为虚拟,我如何使我的
插入器和提取器多态?
Since these operators can''t be member functions, and since
friend functions can''t be declared virtual, how do I make my
inserters and extractors polymorphic?
由运营商超载,例如:
ostream&运营商LT;< (ostream& out,const Base& base);
ostream&运营商LT;< (ostream& out,const Derived& derived);
....
祝福,
Roland Pibinger
by operator overloading, eg.
ostream& operator<< (ostream& out, const Base& base);
ostream& operator<< (ostream& out, const Derived& derived);
....
Best wishes,
Roland Pibinger
我明白了。谢谢Victor。
--Randy
I get it. Thanks Victor.
--Randy
这篇关于插件/提取器操作符覆盖是否可以运行时多态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!