使运算符<<虚拟的?

发布于 2024-10-10 01:30:02 字数 280 浏览 5 评论 0 原文

我需要使用虚拟 <<操作员。但是,当我尝试编写:

virtual friend ostream & operator<<(ostream& os,const Advertising& add);

我收到编译器错误

错误 1 ​​错误 C2575:“运算符 <<” : 只能是成员函数和基数 虚拟

如何将该操作员转为虚拟?

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

How can I turn this operator virtual?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

赤濁 2024-10-17 01:30:03

您定义您的运算符 <<调用虚拟打印方法:

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;
        }
}

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;
        }
}
琴流音 2024-10-17 01:30:03

看起来您确实想为类层次结构提供输出功能,如果是这样,您可以提供一个调用虚拟函数的友元运算符<<

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
    }
};

另请参阅 C++ 常见问题解答

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.

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

星星的軌跡 2024-10-17 01:30:02

此设置的问题在于您上面定义的运算符<<是一个自由函数,它不能是虚拟的(它没有接收者对象)。为了使函数成为虚拟函数,必须将其定义为某个类的成员,这在这里是有问题的,因为如果将 operator<< 定义为类的成员,那么操作数将位于错误的顺序:

class MyClass {
public:
    virtual ostream& operator<< (ostream& out) const;
};

意味着

MyClass myObject;
cout << myObject;

不会编译,但

MyClass myObject;
myObject << cout;

会合法。

要解决此问题,您可以应用软件工程基本定理 - 任何问题都可以通过添加另一层间接来解决。不要将 operator<< 设为虚拟,而是考虑向类中添加一个新的虚函数,如下所示:

class MyClass {
public:
    virtual void print(ostream& where) const;
};

然后,将 operator<< 定义为

ostream& operator<< (ostream& out, const MyClass& mc) {
    mc.print(out);
    return out;
}

这样,< code>operator<< 自由函数具有正确的参数顺序,但 operator<< 的行为可以在子类中自定义。

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:

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:

class MyClass {
public:
    virtual void print(ostream& where) const;
};

Then, define operator<< as

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.

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