重载运算符和编写操纵器?
好吧,我无法让这段代码工作: 我想连接我的自定义操纵器。 因此它们将被称为 cout << endl 被调用。 例如我想要这个:
emit << event1 << event2 << event3;
这是我的代码:
class Emit
{
public:
// ...
const void operator<<(const Event& _event) const;
}const emit; // note this global
inline const void Emit::operator<<(const Event& _event) const
{
Start(_event);
}
class Event
{
// ...
const Event& Event::operator<<(const Event& _event) const;
};
inline const Event& Event::operator<<(const Event& _event) const
{
return _event;
}
但是我不能这样称呼:
emit << event1 << event2 << event3;
我正在收到编译时错误、链接时错误以及我在代码中进行的任何更改,我都会收到相应的错误,但没有成功。
例如这个:
错误 1 错误 C2679:二进制 '<<' : 没有找到需要 a 的运算符 'const EventHandling::Event' 类型的右侧操作数(或者有 没有可接受的转换)c:\users\admin\documents\visual studio 2010\projects\cppsystem\eventhandling\test.h 18
非常感谢。
Ok, I can't get this code work:
I want to concatenate my custom manipulators.
so they will be called like cout << endl
is called.
for example I want this:
emit << event1 << event2 << event3;
here is my code:
class Emit
{
public:
// ...
const void operator<<(const Event& _event) const;
}const emit; // note this global
inline const void Emit::operator<<(const Event& _event) const
{
Start(_event);
}
class Event
{
// ...
const Event& Event::operator<<(const Event& _event) const;
};
inline const Event& Event::operator<<(const Event& _event) const
{
return _event;
}
However I cant call this:
emit << event1 << event2 << event3;
I'm eather receiving compile time error, link time errors and what ever I change in my code I get coresponding error no success.
for example this one:
Error 1 error C2679: binary '<<' : no operator found which takes a
right-hand operand of type 'const EventHandling::Event' (or there is
no acceptable conversion) c:\users\admin\documents\visual studio
2010\projects\cppsystem\eventhandling\test.h 18
thanks alot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些运算符是从左到右调用的。因此,第一次调用 (
emit << event1
) 必须返回对Emit
的引用:现在您不需要重载
operator<< ;
不再在您的Event
类中。Those operators are called from left to right. As such, the first call (
emit << event1
) must return a reference toEmit
:And now you don't need to overload
operator<<
in yourEvent
class anymore.如果您的操纵器不包含任何数据,您实际上可以只编写一个函数。例如, std::endl 的实现大致如下(它也需要处理宽流,因此会执行一些魔法来安排字符转换):
如果您的操纵器有一些您需要的数据将数据存储在合适的对象中,然后为此类创建一个普通的输出运算符,例如
std::setw()
可以像这样实现(再次忽略流实际上是模板):您可以将输出运算符实现为成员,因为您无法控制运算符<<()的左侧:这是流对象所在的位置。如果您正在实现 std::ostream ,您可以实现这些成员(实际上,标准要求某些输出运算符是 std::ostream 的成员)。
If your manipulators don't contain any data you can actually just write a function. For example,
std::endl
is implemented roughly like this (it needs to cope with wide streams as well and thus does some magic to arrange for characters be converted):If your manipulators have some data you need to store the data in a suitable object and then just create a normal output operator for this class, for example
std::setw()
could be implemented like this (again, ignoring that the streams are actually templates):You can implement output operators as a member because you don't control the left side of the
operator<<()
: this is where the stream object sits. If you were implementingstd::ostream
you could implement these members (well, actually, the standard mandates that certain output operators are members ofstd::ostream
).