子コンストラクターの初期化リストで基本クラスの 1 つのアドレスを取得する標準的で安全な方法はありますか?
これが私がやりたいことです:
バッファを処理する機能を提供する複数のクラス (void*、長さ) があり、データを保持する複数のクラス (C スタイルの構造体) があります。最小限のコードで両方を組み合わせてユーザーに提供したいと考えています。これが私が考えていることです:
//Parent1 is POD (old c-style structure)
struct Parent1{/*C style stuff*/};
//Parent2 and Parent3 are NOT POD (virtual functions, copy constructors, ...)
class Parent2{public: Parent2(void*, size_t);/*stuff*/};
class Parent3{public: Parent3(void*, size_t);/*stuff*/};
/*order of inhertiance is not guranteed, it could be Parent2, Parent2, Parent3*/
struct Child1 : public Parent1, public Parent2, public Parent3
{
Child1()
: Parent2((Parent1*)this, sizeof(Parent1)),
Parent3((Parent1*)this, sizeof(Parent1))
{
}
//using self() instead of this is trick which I don't prefer to use.
const Child1* self()const{return this;}
};
これにより、gcc は正常にコンパイルされますが、Visual Studio では警告が表示されます。後で他の組み込みコンパイラを試してみます。MISRA C++ チェック、ポリスペースチェック、およびその他の静的解析ツールに合格できる標準ソリューションを探しています。
編集:
Visual Studio の警告: 警告 C4355: 'this': 基本メンバー初期化子リストで使用