class Computer {
public:
Computer(CPU* c1, GPU* g1, Memory* m1) {
m_c1 = c1;
m_g1 = g1;
m_m1 = m1;
}
~Computer() {
if (m_c1 != NULL) {
delete m_c1;
m_c1 = NULL;
}//试了好多次,发现只能删除m_c1,删除其他两个参数都会报错呢
//if (m_g1 != NULL) {
// delete m_g1;
//}
//if (m_m1 != NULL) {
// delete m_m1;
//}
}
void runCom() {
m_c1->computer();
m_g1->display();
m_m1->memory();
}
CPU* m_c1;
GPU* m_g1;
Memory* m_m1;
};
class Intel: public GPU, public Memory, public CPU {
public:
virtual void computer() {
cout << "Intel CPU is runnung" << endl;
}
virtual void display() {
cout << "Intel GPU is runnung" << endl;
}
virtual void memory() {
cout << "Intel Memory is runnung" << endl;
}
};
class Lenovo : public CPU, public GPU, public Memory {
public:
virtual void computer() {
cout << "Lenovo CPU is runnung" << endl;
}
virtual void display() {
cout << "Lenovo GPU is runnung" << endl;
}
virtual void memory() {
cout << "Lenovo Memory is runnung" << endl;
}
};
void test01() {
CPU* c1 = new Lenovo;
Computer com1(c1, new Intel, new Intel);
com1.runCom();
//delete c1;
}
在构造函数中三个形参,推测是多继承引起的问问题,原因不知道,现在还没有解决。