如何制作这个指针向量,然后正确打印 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 定义单独的运算符(这一点在评论之一中指出)
Since std::ostream
是一个超类std::ofstream
,只需要实现一个运算符std::ostream
.
2)“使用命名空间std”总是做错事 https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice.
3)main()函数应该返回一个 int https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c.
现在,要回答这里的主要问题,这里需要做的就是使用虚拟继承。这就是虚拟继承的用途。你懂的。现在,正如您所指出的,仅仅因为运算符不能是虚拟的,绝对没有法律可以阻止他们自己调用虚拟方法。
Base
's 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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)