上播和流运算符重载
Upcasting and Stream Operator Overloading
如您所见,在两个实例上仅调用基类的流插入运算符的重载版本。我明白为什么会这样。这是因为没有动态绑定。但是,我该如何解决它?
#include <iostream>
using namespace std;
class A {
int i;
char c;
public:
A(int i = 0, char c = ' ') {
this->i = i;
this->c = c;
}
int getI() { return i; }
char getC() { return c; }
friend ostream& operator << (ostream&, A&);
};
class B : public A {
double d;
public:
B(int i = 0, char c = ' ', double d = 0.0) : A(i, c), d(d) {}
friend ostream& operator << (ostream&, B&);
};
ostream& operator << (ostream& out, A& a) {
out << "nInteger: " << a.i << "nCharacter: " << a.c << endl;
return out;
}
ostream& operator << (ostream& out, B& b) {
out << "nInteger: " << b.getI() << "nCharacter: " << b.getC() << "nDouble: " << b.d << endl;
return out;
}
int main() {
A* a = new A (10, 'x');
B* b = new B(20, 'y', 5.23);
A* array[] = { a, b };
cout << *(array[0]);
cout << "n______________________________n";
cout << *(array[1]);
delete a;
delete b;
cin.get();
return 0;
}
如何使cout << *(array[1]);
调用将 B 的对象作为其参数之一的重载流插入运算符?
你不能让它调用重载运算符,因为重载是在编译时解决的。
要在运行时进行解析,即使用动态调度,您需要将执行打印的代码移动到虚拟成员函数。
然后从运算符调用它(对于基类,你只需要一个)。
class A
{
public:
// ...
// Override this in B
virtual void print(std::ostream& o) const
{
o << "nInteger: " << i << "nCharacter: " << c << endl;
}
// ...
};
ostream& operator << (std::ostream& out, const A& a) {
a.print(out);
return out;
}
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板