C++ - 相互对象包含
C++ - mutual object include
我运行遇到以下问题:
class School
{
Manager manager;
}
class Manager
{
School school;
}
它将需要无限内存,并会导致不必要的无限循环。
最好的解决方法是什么?
class School;
class Manager
{
std::weak_ptr<School> school;
};
class School
{
std::shared_ptr<Manager> manager;
};
根据您想要执行的操作,您可能希望使用 Manager 中的 shared_ptr 和 School 中的 weak_ptr,但概念保持不变。
我运行遇到以下问题:
class School
{
Manager manager;
}
class Manager
{
School school;
}
它将需要无限内存,并会导致不必要的无限循环。
最好的解决方法是什么?
class School;
class Manager
{
std::weak_ptr<School> school;
};
class School
{
std::shared_ptr<Manager> manager;
};
根据您想要执行的操作,您可能希望使用 Manager 中的 shared_ptr 和 School 中的 weak_ptr,但概念保持不变。