我们如何将Ostream函数作为类的成员函数实现,而不是作为朋友函数,以便我可以用作虚拟函数
How can we implement ostream function as member function of class ,and not as friend function so that i can be used as virtual function?
我想使用friend ostream&基类的功能,我们如何做到?如何使成员功能而不是类的成员函数,以便它可以用作虚拟函数并显示派生类的详细信息。
流插入器将流作为其第一个参数,因此当插入器是成员函数时,它是流类的成员,而不是您正在编写的类。因此,它不能是您班上的虚拟功能。但是它可以呼叫您的课程中的虚拟功能。
class my_class {
virtual std::ostream& put(std::ostream& out) const {
out << "here we go!b";
}
friend std::ostream& operator<<(std::ostream& out, const my_class& my_object);
};
std::ostream& operator<<(std::ostream& out, const my_class& my_object) {
my_object.put(out);
}
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 我可以在 C++ 中的函数体之外进行操作吗?
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 无论如何,我可以确定构造函数是否存在吗?
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 我可以将向量作为initial_sum和不同的函数传递给 std::累积吗?
- 我可以这样调用函数吗?
- 如果我在 const 函数上使用指针,我可以返回什么?
- 我可以在Visual Studio 2019上的项目下使用多个主函数吗?
- 我可以在构造函数的主体中转发构造吗?
- 我如何获取数组的大小,以便我可以从函数声明所述数组
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 我可以在类中存储构造函数的模板参数吗?
- 我可以动态创建新地图并作为函数参数传递吗?
- 我可以调用从 main() 覆盖的虚拟函数吗?
- 我可以使用在类构造函数中初始化的流类型的成员变量吗?
- 我可以在函数中调用该类中的类吗?
- 我可以使用谷歌 v8 在回调函数中获取 JavaScript 函数C++源文本吗?
- 我可以说服自动生成的构造函数将我的 POD 类成员归零吗?