列挙型の宣言や文字列の使用を避けようとしています。そうする理由は疑わしいように思われるかもしれませんが、完全な説明は無関係です。
私の質問はかなり単純です。メンバー変数のアドレスを一意のIDとして使用できますか?
より具体的には、要件は次のとおりです。
- IDをシリアル化する必要はありません。
- IDは保護されたメンバーになります-所有するオブジェクトによって内部的にのみ使用されます(同じクラスインスタンス間でもIDの比較はありません)。
- サブクラスは基本クラスIDにアクセスする必要があり、新しいIDを追加できます。
したがって、最初の解決策は次のとおりです。
class SomeClass
{
public:
int mBlacks;
void AddBlack( int aAge )
{
// Can &mBlacks be treated as a unique ID?
// Will this always work?
// Is void* the right type?
void *iId = &mBlacks;
// Do something with iId and aAge
// Like push a struct of both to a vector.
}
};
2番目の解決策はこれですが:
class SomeClass
{
public:
static int const *GetBlacksId()
{
static const int dummy = 0;
return &dummy;
}
void AddBlack( int aAge )
{
// Do something with GetBlacksId and aAge
// Like push a struct of both to a vector.
}
};