C++ 使运算符<< 虚拟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4571611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making operator<< virtual?
提问by inna karpasas
I need to use a virtual << operator. However, when I try to write:
我需要使用虚拟 << 运算符。但是,当我尝试编写时:
virtual friend ostream & operator<<(ostream& os,const Advertising& add);
I get the compiler error
我收到编译器错误
Error 1 error C2575: 'operator <<' : only member functions and bases can be virtual
错误 1 错误 C2575:'operator <<':只有成员函数和基类可以是虚拟的
How can I turn this operator virtual?
我怎样才能把这个操作员变成虚拟的?
回答by templatetypedef
The problem with this setup is that the operator<< you defined above is a free function, which can't be virtual (it has no receiver object). In order to make the function virtual, it must be defined as a member of some class, which is problematic here because if you define operator<< as a member of a class then the operands will be in the wrong order:
此设置的问题在于您在上面定义的 operator<< 是一个自由函数,它不能是虚拟的(它没有接收器对象)。为了使函数成为虚拟函数,它必须被定义为某个类的成员,这在这里是有问题的,因为如果将 operator<< 定义为类的成员,那么操作数的顺序将是错误的:
class MyClass {
public:
virtual ostream& operator<< (ostream& out) const;
};
means that
意思是
MyClass myObject;
cout << myObject;
will not compile, but
不会编译,但是
MyClass myObject;
myObject << cout;
will be legal.
将是合法的。
To fix this, you can apply the Fundamental Theorem of Software Engineering - any problem can be solved by adding another layer of indirection. Rather than making operator<< virtual, consider adding a new virtual function to the class that looks like this:
为了解决这个问题,您可以应用软件工程的基本定理——任何问题都可以通过添加另一层间接来解决。与其将 operator<< 设为虚拟,不如考虑向类中添加一个新的虚拟函数,如下所示:
class MyClass {
public:
virtual void print(ostream& where) const;
};
Then, define operator<< as
然后,将 operator<< 定义为
ostream& operator<< (ostream& out, const MyClass& mc) {
mc.print(out);
return out;
}
This way, the operator<< free function has the right parameter order, but the behavior of operator<< can be customized in subclasses.
这样,operator<< free 函数有正确的参数顺序,但是operator<< 的行为可以在子类中自定义。
Hope this helps!
希望这可以帮助!
回答by Martin York
You define your operator << to call a virtual print method:
您定义运算符 << 来调用虚拟打印方法:
class Base
{
protected:
virtual void print(std::ostream& str) const = 0;
public:
friend std::ostream& operator<<(std::ostream& str, Base const& data)
{
data.print(str);
return str;
}
}
回答by wkl
It looks like you really want to provide output functionality for a hierarchy of classes, and if so, you can provide a friend operator <<
that calls a virtual
function.
看起来您真的想为类的层次结构提供输出功能,如果是这样,您可以提供friend operator <<
调用virtual
函数的 。
class Parent
{
public:
friend std::ostream& operator<< (std::ostream& os, const Parent& p);
// ... other class stuff
protected:
virtual void printMyself(std::ostream& os) const
{
// do something if you must, or make this a pure virtual
}
};
std::ostream& operator<< (std::ostream& os, const Parent& p)
{
p.printMyself(os);
return os;
}
class Child : public Parent
{
// other class stuff...
protected:
virtual void printMyself(std::ostream os) const
{
// whatever you need to do
}
};
Also detailed in the C++ FAQ
在C++ FAQ 中也有详细说明