引用: C++のためのAPIデザイン by マーティン・レディ
第7章 パフォーマンス
クラス内のこの定数を宣言できる場合は、もっと優れた方法がある。つまり、静的定数として宣言すればよいのだ(オブジェクトごとのメモリ容量に勘定されなくなる)。
myapi.h
class MyAPI
{
public:
static const int MAX_NAME_LENGTH;
static const int MAX_RECORDS;
static const std::string LOG_FILENAME;
};
これで、関連する.cppファイルでこれらの定数用の値を定義できる。
myapi.cpp
const int MyAPI::MAX_NAME_LENGTH = 128;
const int MyAPI::MAX_RECORDS = 65525;
const std::string MyAPI::LOG_FILENAME = "filename.log";