gpt4 book ai didi

c++ - 虚拟 << 运算符

转载 作者:行者123 更新时间:2023-11-30 03:02:26 28 4
gpt4 key购买 nike

我有一个小问题,我的 << 运算符没有被正确调用。

这是我的:

class SomeInterface
{
friend std::ostream& operator<<(std::ostream& str, const SomeInterface& data);

protected:
virtual void print(ostream& str) const = 0;
};

inline std::ostream& operator<< (std::ostream& o, SomeInterface const& b)
{
b.print(o);
return o;
}
}

调用代码如下所示:

SomeInterface* one  = new someConcrete ();
cout << one;

我希望在接口(interface)上调用的 << 重载函数没有,更不用说派生类了。

最佳答案

尝试:

cout << *one;

您的代码要求打印指针,而您的operator<<需要 const SomeInterface&引用。

关于c++ - 虚拟 << 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10166779/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com