は以下の不自然な例を考えてみましょう:クラスの複数のインスタンスを動的に割り当てられたオブジェクトのリストを共有する必要があるかどうかはさておき実装ファイルで静的マップに動的に割り当てられたオブジェクト。削除は必要ですか?
class AllocatedClass {
public:
AllocatedClass() : dummy(0) {}
private:
int dummy;
};
class AllocatingClass {
public:
AllocatingClass() : list() {}
~AllocatingClass() {
// CANNOT delete the elements in list here because there may
// be more than on instance of AllocatingClass sharing the same
// static list
}
AddNewObject() {
list.push_back(new AllocatedClass());
}
private:
static std::vector<AllocatedClass*> list;
};
std::vector<AllocatedClass*> AllocatingClass::list;
は、良いアイデアですプログラムの終わりにこれらの新しいAllocatedClassオブジェクトをクリーンアップする方法がありますか?アプリケーションの終了時まで削除したくないと考えれば、削除されることは決してないでしょうか?
[std :: shared_ptr](http://en.cppreference.com/w/cpp/memory/shared_ptr)の使用はどうですか? – WhiZTiM
すべての 'new()'はそれに対応する 'delete'を見る必要があります。 –
このコードは単純にコンパイルできません。 VTC。 – SergeyA