65,208
社区成员
发帖
与我相关
我的任务
分享
,,,,
struct Shape {
virtual ~Shape() noexcept { }
virtual void out(std::ostream& os) const = 0;
};
auto operator <<(std::ostream& os, const Shape& s) -> std::ostream& {
s.out(os); return os;
}
建议这样写(返回void),返回 std::ostream这个对象本身就是为了方便代码编写,也方便编译器优化,
假设那个虚函数返回这个对象(std::ostream),不方便编译器优化..class Shape
{
public:
virtual ostream& print(ostream& os) const { /*子类重写这个函数*/ }
friend ostream& operator<<(ostream& os, const Shape& obj) { return obj.print(os); }
};