#include <iostream>
#include<list>
using namespace std;
class Object;
typedef list<Object *> ObjectList;
class Object
{
public:
ObjectList children;
Object(Object * parent = nullptr)
{
if(parent != nullptr)
{
parent->children.push_back(this);
}
}
virtual ~Object()
{
for(auto it=children.begin(); it!=children.end(); it++)
{
delete *it;
}
cout << "删除" << endl;
}
ObjectList &childrenList()
{
return this->children;
}
};
class A:public Object
{
public:
A(Object *parent = nullptr) {
if(parent != nullptr)
{
parent->children.push_back(this);
}
cout<<"A的构造函数"<<endl;
}
~A()
{
cout<<"A的析构函数"<<endl;
}
};
class B:public Object
{
public:
B(Object *parent = nullptr)
{
if(parent != nullptr)
{
parent->children.push_back(this);
}
cout<<"B的构造函数"<<endl;
}
~B()
{
cout<<"B的析构函数"<<endl;
}
};
int main()
{
A a1;
B *b1 = new B(&a1);
return 0;
}
2022-11-14
最新推荐文章于 2024-09-08 21:26:22 发布