私は大きな構造体を実装しています(構造体Aに構造体Bがあり、構造体Bに構造体Cなどがあります)。いずれかのネストされたレベルでは、1つの構造体にはそれ自身へのポインタがあります。これでどのソリューションを管理するのが良いでしょうか?共有ポインタを持つリストの共有ポインタ
struct MyStruct
{
//1st version
std::shared_ptr<std::list<std::shared_ptr<MyStruct>>> myStructPrtList;
//2nd version
std::list<MyStruct*> *myStructPrtList;
};
ローポインタによる管理では、オブジェクトの作成と削除が必要です。スマートポインタはどうですか?そのようなリストを作成すると危険ですか?
EDIT
これは、この構造体が使用される方法です。
void createStruct()
{
//Here will be created pointer to main struct
std::shared_ptr<StructA> structA = fillStruct();
//Here get everything what is needed from this struct and this struct wont be used nymore
}
fillStruct機能:
std::shared_ptr<StructA> fillStruct()
{
std::shared_ptr<StructA> structA = std::make_shared<StructA>();
//Here fill whole structA
//Somewhere create struct B included in struct A
structA->structB = std::make_shared<StructB>();
//Now fill struct B
//and so on...
//Now somewhere in nested struct create MyStruct
//return this whole created struct
return structA;
}
は、あなたの答えをいただき、ありがとうございます。
これは、あなたが使用しようとしている生涯管理/要件を説明することなく答えられるとは思えません。それについて考えると、おそらくあなた自身の質問に答えるでしょう。 –
パフォーマンスが重大ではない場合、私は常に第1版を第2版より優先します。自動的にあなたのために行うことができる場合、あなたはなぜ自分自身を管理しますか? また、 'std :: vector'よりも' std :: list'を使いたいのですか? – Timo
@Timo: 'shared_ptr'は本格的なガベージコレクションではありません。それはイントロスペクションを行うことはできず、サイクルを検出できないため、サイクルを中断することはできません。 – MSalters