簡体スニップ:この作品インスタンス化せずにコンストラクタの呼び出しを防止できますか?私のクラスの
class UpdatingSystem
/* when system is "unsafe" it marks this condition by instantiating an
UpdatingSystem item. While this item exists errors are handled in a
particular way. When the UpdatingSystem item goes out of scope error
handling reverts to normal.*/
{
private:
static THREAD int updatingSystemCount_;
public:
UpdatingSystem(const char* caller){updatingSystemCount++};
~UpdatingSystem(){updatingSystemCount--};
};
:
{
UpdatingSystem tempUnsafe("Reason it's unsafe");
// do stuff that requires the system be 'unsafe'
} // Revert to safe when destructor runs
これにはない:
{
UpdatingSystem("Reason it's unsafe");
// do stuff that requires the system be 'unsafe'
}
...と呼ばれるコンストラクタは、一時的な項目と作成されますので、デストラクタはすぐに実行され、システムを「安全」な状態にしないでください。
2番目の欠陥のあるバージョンを書き込むのは間違いやすいです。 インスタンス化せずにそのようなコンストラクタの呼び出しを防ぐ方法はありますか?
これを防ぐ手段はありません。 C++は匿名の一時ファイルを許可しますが、あなたはそれについて何もできません。 –
いいえ、クラスは、オブジェクトが左辺値に保存されていることを知る方法がありません。 –
私はそれを削除しました、ミスは質問を読んで:)その初期のコーヒーはまだ持っています:) – Rob