2011-08-12 3 views
2

Iveはダイヤモンドを手に入れました。私はクラスメンバーにアクセスしたい。私はmingwを使用しています。 "トップ::" メンバーにアクセスする方法を再宣言クラス あいまいなクラスメンバーへのアクセス

#include <cstdio> 
class top { 
public: 
    const char A; 
    top(): A('t') {} 
}; 
class left: public top { 
public: 
    const char A; 
    left():A('l'){} 
}; 
class right: public top {}; 
class bottom: public left, public right {}; 

int main() { 
    bottom obj; 
    printf("%c\n", obj.bottom::right::A); //using right::A, inherited from top::A 
    printf("%c\n", obj.bottom::left::A); //using left::A and left::top::A is hidden 
    //printf("%c\n", obj.bottom::left::top::A); //error. How to access it? 
    return 0; 
} 

私はコメントmingwの削除私にエラー与え

なし::

'top' is an ambiguous base of 'bottom' 

更新:
質問があるように見えますキャストタイプの作品:

printf("%c\n", static_cast<top>(static_cast<left>(obj)).A); 
printf("%c\n", static_cast<left>(obj).::top::A); 
printf("%c\n", reinterpret_cast<top&>(obj).A);//considered bad 
printf("%c\n", (reinterpret_cast<top*>(&obj))->A);//considered evil 
//  printf("%c\n", static_cast<top&>(obj).A);//error 
+0

Annotated C++リファレンスマニュアルの10.11c節を参照してください。また、あなたの相続財産にダイヤモンドを持たないでください。これは悪いです。 –

+0

仮想継承について知りたいのですか、実際には 'top'の2つの別々の非仮想コピーにアクセスする方法を実際に知りたいのですか? –

+0

@Kerrekは、実際には、メモリにはトップの2つの別々の非バーチャルコピーにアクセスする方法が必要です。 – all

答えて

4

を参照してください:

printf("%c\n", static_cast<left&>(obj).::top::A); 
-1

Dimond probl em。あなたは正しい基本クラスを選択するコンパイラを説得するタイプを少しマッサージすることができ、仮想継承のために行くがなければhttp://en.wikipedia.org/wiki/Diamond_problem

+0

私はそれを見ました。しかし、オブジェクトにはobj.bottom :: left :: top :: Aがメモリに入っています – all

2

私はかなりC++第一人者ないんだけど、ないかもしれません次の作業は?

left &asLeft = obj ; 
top &asTop = asLeft ; 
cout << asTop.A << endl ; 
関連する問題