次のコードがコンパイル時に「一時的な参照を返す」と失敗する理由がわかりません。私にとって、シングルトンは静的であるため、一時的なものにすることはできません!?
ありがとう
#include <memory>
class Parent {
public:
static const std::shared_ptr<Parent>& get_default_value();
static const std::shared_ptr<Parent>& get_some_other_value();
};
class Child: public Parent {
public:
Child(){}
static const std::shared_ptr<Child>& singleton;
};
const std::shared_ptr<Child>& singleton = std::make_shared<Child>();
const std::shared_ptr<Parent>& Parent::get_default_value() {
return singleton;
}
const std::shared_ptr<Parent>& Parent::get_some_other_value() {
//FILL ME
}
編集:親のデフォルト値は子のシングルトンです。(以前は他の名前もありましたが、これは混乱を招きました)。
編集2:また、デフォルトはLOTであり、とにかくシングルトンであるため、shared_pointersへの参照が必要でした。スペースを節約したほうがよいでしょう。
編集3:インターフェイスをデフォルトと他の値で一貫させたいので、タイプの結果としてstd :: shared_ptr&が必要です
編集4:そして、無関係な理由で、他の値はshared_ptr<>である必要があります。