プロジェクトのさまざまなクラスで使用される文字列定数を宣言したいと思います。私は2つの選択肢を検討しています
オプション1:
#header file
class constants{
static const string const1;
};
#cpp file
const string constants::const1="blah";
オプション2:
#header file
namespace constants{
static const string const1="blah";
};
何がより良い実装になるのか疑問に思っています。
すでに見た
アップデート:
オプション3:
「potatoswatter」と「sellibitze」からの提案に基づいて、私は現在次の実装を持っていますか?
#header file
namespace constants{
extern const string& const1(); //WORKS WITHOUT THE EXTERN ***WHY***
};
#cpp file
namespace constants{
const string& const1(){static string* str = new string ("blah"); return *str;}
}
定数を使用する必要があるヘッダーファイルをインクルードしています。この実装の主な短所はありますか?