使用引用此对象的基类指针向量打印派生类对象
Print Derived class object using a vector of Base class pointers referring this object
如何制作这个指针向量,然后正确打印 de Derived1 对象和 Derived2 对象?
即使我在派生类中包含"<<"运算符,似乎也使用了基类运算符。而且我不能使操作员成为虚拟的,因为他们不是类的成员,他们是朋友。
我该怎么做才能使程序从 Derived1 类或 Derived2 类中获取"<<"运算符,此外,通过使用打印基类的东西:
out << (Base&)temp;
在我的运营商的定义中.
#include<iostream>
#include<fstream>
using namespace std;
class Base {
private:
int base;
public:
Base() :base(10){}
friend ostream& operator<<(ostream& out, const Base& temp)
{
out << "======== BASE ========" << endl;
out << temp.base << endl;
out << "======== BASE ========" << endl;
return out;
}
friend ofstream& operator<<(ofstream& out, const Base& temp)
{
out << "======== BASE ========" << endl;
out << temp.base << endl;
out << "======== BASE ========" << endl;
return out;
}
};
class Derived1 :public Base {
private :
int derived1;
public:
Derived1() :derived1(5){}
friend ostream& operator<<(ostream& out, const Derived1& temp)
{
out << (Base&)temp;
out << "======== DERIVED1 ========" << endl;
out << temp.derived1 << endl;
out << "======== DERIVED1 ========" << endl;
return out;
}
friend ofstream& operator<<(ofstream& out, const Derived1& temp)
{
out << (Base&)temp;
out << "======== DERIVED1 ========" << endl;
out << temp.derived1 << endl;
out << "======== DERIVED1 ========" << endl;
return out;
}
};
class Derived2 :public Base {
private:
int derived2;
public:
Derived2() :derived2(5) {}
friend ostream& operator<<(ostream& out, const Derived2& temp)
{
out << (Base&)temp;
out << "======== DERIVED2 ========" << endl;
out << temp.derived2 << endl;
out << "======== DERIVED2 ========" << endl;
return out;
}
friend ofstream& operator<<(ofstream& out, const Derived2& temp)
{
out << (Base&)temp;
out << "======== DERIVED2 ========" << endl;
out << temp.derived2 << endl;
out << "======== DERIVED2 ========" << endl;
return out;
}
};
void main()
{
Derived1 d1;
Derived2 d2;
Base* v[2];
v[0] = &d1;
v[1] = &d2;
cout << *v[0] << endl;
ofstream f("fis.txt");
f << *v[0];
}
显示的代码存在几个问题
1)没有必要为std::ostream和std::ofstream定义单独的运算符(这在其中一个评论中有所说明)
由于std::ostream
是std::ofstream
的超类,所以只需要为std::ostream
实现一个运算符。
2)"使用命名空间标准"总是错误的做法。
3) main() 函数应该返回一个 int。
现在,为了回答这里的主要问题,这里需要发生的只是使用虚拟继承。这就是虚拟继承的用途。你知道的。现在,正如您所指出的,仅仅因为运算符不能是虚拟的,绝对没有法律阻止他们自己调用虚拟方法。
Base
的operator<<
应该只是一些适当命名的函数的门面,例如format(),它将在类中作为虚拟方法实现。
最终结果如下所示:
#include<iostream>
#include<fstream>
class Base {
private:
int base;
public:
Base() :base(10){}
friend std::ostream& operator<<(std::ostream& out, const Base& temp)
{
temp.format(out);
return out;
}
virtual void format(std::ostream &out) const
{
out << "======== BASE ========" << std::endl;
out << base << std::endl;
out << "======== BASE ========" << std::endl;
}
};
class Derived1 :public Base {
private :
int derived1;
public:
Derived1() :derived1(5){}
void format(std::ostream &out) const override
{
out << "======== DERIVED1 ========" << std::endl;
out << derived1 << std::endl;
out << "======== DERIVED1 ========" << std::endl;
}
};
class Derived2 :public Base {
private:
int derived2;
public:
Derived2() :derived2(5) {}
void format(std::ostream &out) const override
{
out << "======== DERIVED1 ========" << std::endl;
out << derived2 << std::endl;
out << "======== DERIVED1 ========" << std::endl;
}
};
int main()
{
Derived1 d1;
Derived2 d2;
Base* v[2];
v[0] = &d1;
v[1] = &d2;
std::cout << *v[0] << std::endl;
std::ofstream f("fis.txt");
f << *v[0];
}
由于您无法使流式处理运算符工作,因为它不是成员函数,因此无法成为虚拟函数,因此您必须使另一个函数成为虚拟函数,并从基的流运算符调用它。你可以称之为virtual void print()
相关文章:
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 从多个源构造一个对象,包括一个对象向量
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 对象 C++ 向量的 STL 容器
- 初始化C++中的对象向量
- 从自定义类获取对象向量中的 max 元素
- 如何在 c++ 中打印存档中的对象向量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 尝试重载输出运算符时,我无法遍历对象向量
- 如何在C++中打印对象向量的内容
- 打印出对象向量中的每个元素C++
- 在 c++ 的构造函数中分配对象向量时出错
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 对象向量是否在堆或堆栈上分配C++?
- 基于不同字段的对象向量的排序功能
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- 用rapidjson读取子对象向量
- 尝试使用比较运算符对对象向量进行排序
- 如何用对象向量重载 cin(>>)
- 如何将某个元素从shared_ptr擦除到对象向量?