gpt4 book ai didi

c++ - 以支持继承的方式在对象的构造函数中将 shared_ptr 添加到 self to vector

转载 作者:行者123 更新时间:2023-11-30 03:22:32 25 4
gpt4 key购买 nike

我希望在创建对象时将对已创建对象的引用自动添加到 vector (通常是多个 vector )中。为了提供一些上下文,此代码将在游戏中用于游戏对象集(Drawable、Collidable、Enemies 等),因此需要多个 vector 。

此处显示了我正在尝试实现的示例:

#include <iostream>
#include <memory>
#include <vector>
class BaseClass :public std::enable_shared_from_this<BaseClass>
{ //for example "drawable"
public:
BaseClass()
{
std::cout << "Base Created"<<std::endl;
BaseList.push_back(shared_from_this());//I want to put a reference to this object in a vector
}
~BaseClass()
{
std::cout << "Base Deleted"<<std::endl;
}
static std::vector<std::shared_ptr<BaseClass>> BaseList;

};
class DerivedClass :public BaseClass
{//for example "enemy"
public:
static std::vector<std::shared_ptr<BaseClass>> DerivedList; //shared_ptr of baseclass can point to derivedclass
DerivedClass()
{
std::cout << "Derived Created" << std::endl;
DerivedList.push_back(shared_from_this());//reference to this object in a vector in addition to the other vector
}
~DerivedClass()
{
std::cout << "Derived Deleted" << std::endl;
}
};
std::vector<std::shared_ptr<BaseClass>> BaseClass::BaseList;
std::vector<std::shared_ptr<BaseClass>> DerivedClass::DerivedList;
int main()
{

std::shared_ptr<BaseClass> C = std::make_shared<BaseClass>();
std::shared_ptr<BaseClass> D = std::make_shared<DerivedClass>();

BaseClass::BaseList.clear(); //C should be deleted, D should not since it is still in DerivedList
DerivedClass::DerivedList.clear(); //now D should be deleted
return 0;
}

在此代码中,使用shared_from_this()无法正常工作,因为它在构造函数中 ( As Shown Here )。以前我已经使用单独的静态函数解决了这个问题,例如:

void BaseClass::makeOne()
{
std::shared_ptr<BaseClass> P(new BaseClass());
BaseClass::BaseList.push_back(P);
}

void DerivedClass::makeOne()
{
std::shared_ptr<BaseClass> P(new DerivedClass());
BaseList.push_back(P);
DerivedList.push_back(P);
}

然而,在多个类从单个基类派生的上下文中,并且每个派生类也可能被添加到其他 vector 中,代码重复成为一个问题(应该为每个继承 BaseList.push_back(P) 的对象调用 BaseClass ,因此必须写在每个 X::MakeOne() 中,其中 X 继承 BaseClass )。

我也通过简单地使用原始指针 (std::vector<BaseClass*>) 克服了这个问题,但是当在多个地方引用对象时,这失去了简单内存管理和引用计数的好处。在这种情况下是否有更好的内存管理选项?

最佳答案

我认为这可以通过使用 Factory/Provider 来解决。

class SomeBaseClass // Could also be an interface or similar
{
};

// Instead of having SomeBaseClassFactory instance, all the methods could also be static
class SomeBaseClassFactory
{
public:
std::vector<std::shared_ptr<SomeBaseClass>> someBaseClassList;

std::shared_ptr<SomeBaseClass> GenerateObject(/* Parameters */)
{
std::shared_ptr<SomeBaseClass> someBaseClass = std::make_shared<SomeBaseClass>(/* Parameters */);
RegisterObject(someBaseClass);
return someBaseClass;
}

protected:
void RegisterObject(std::shared_ptr<SomeBaseClass> objectToRegister)
{
someBaseClassList.push_back(objectToRegister);
}
};

class SomeDerivedClass : public SomeBaseClass
{
};

class SomeDerivedClassFactory : public SomeBaseClassFactory
{
public:
std::shared_ptr<SomeDerivedClass> GenerateObject(/* Parameters */)
{
std::shared_ptr<SomeDerivedClass> someDerivedClass = std::make_shared<SomeDerivedClass>(/* Parameters */);
RegisterObject(someDerivedClass);
return someDerivedClass;
}
};

关于c++ - 以支持继承的方式在对象的构造函数中将 shared_ptr 添加到 self to vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51066435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com