重载运算符和编写操纵器?

发布于 2024-12-25 06:05:53 字数 1092 浏览 0 评论 0原文

好吧,我无法让这段代码工作: 我想连接我的自定义操纵器。 因此它们将被称为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2025-01-01 06:05:53

这些运算符是从左到右调用的。因此,第一次调用 (emit << event1) 必须返回对 Emit 的引用:

class Emit
{
public:
    // ...
    Emit const& operator<<(const Event& _event) const;
}const emit; // note this global

Emit const& Emit::operator<<(const Event& _event) const
{
    Start(_event);
    return *this;
}

现在您不需要重载 operator<< ; 不再在您的 Event 类中。

Those operators are called from left to right. As such, the first call (emit << event1) must return a reference to Emit:

class Emit
{
public:
    // ...
    Emit const& operator<<(const Event& _event) const;
}const emit; // note this global

Emit const& Emit::operator<<(const Event& _event) const
{
    Start(_event);
    return *this;
}

And now you don't need to overload operator<< in your Event class anymore.

初与友歌 2025-01-01 06:05:53

如果您的操纵器不包含任何数据,您实际上可以只编写一个函数。例如, std::endl 的实现大致如下(它也需要处理宽流,因此会执行一些魔法来安排字符转换):

std::ostream& endl(std::ostream& out) {
    (out << '\n').flush();
    return out;
}

如果您的操纵器有一些您需要的数据将数据存储在合适的对象中,然后为此类创建一个普通的输出运算符,例如 std::setw() 可以像这样实现(再次忽略流实际上是模板):

struct std::setw {
    setw(int size): size_(size) {}
    int size_;
};
std::ostream& operator<< (std::ostream& out, std::setw const& object) {
    out.width(object.size_);
    return out;
}

您可以将输出运算符实现为成员,因为您无法控制运算符<<()的左侧:这是流对象所在的位置。如果您正在实现 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):

std::ostream& endl(std::ostream& out) {
    (out << '\n').flush();
    return out;
}

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):

struct std::setw {
    setw(int size): size_(size) {}
    int size_;
};
std::ostream& operator<< (std::ostream& out, std::setw const& object) {
    out.width(object.size_);
    return out;
}

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 implementing std::ostream you could implement these members (well, actually, the standard mandates that certain output operators are members of std::ostream).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文