Stroustrup の新しい本を読んでいたところです。Chapter 22.2.2 で、彼は dynamic_cast の問題について議論しています。
これを自分でテストするために書いたコードは次のとおりです。
class Storable
{
public:
int i;
virtual void r() {};
Storable()
{
i = 1;
};
};
class Component:public virtual Storable
{
public:
Component()
{
i = 1;
};
};
class Receiver:public Component
{
public:
Receiver()
{
i = 2;
};
};
class Transmitter:public Component
{
public:
Transmitter()
{
i = 3;
};
};
class Radio:public Transmitter
{
public:
Radio()
{
i = 4;
};
};
int _tmain(int argc, _TCHAR* argv[])
{
Radio *r = new Radio();
Storable *s1 = dynamic_cast<Storable*>(r);
Component *c = dynamic_cast<Component*>(s1); // this should be 0 but it is not!
return 0;
}
Stroostrup は、どのバージョンの Storable が参照されているかを知ることができないため、c は nullptr であるべきだと説明しています。ただし、有効なポインターとして取得します。
これについては Stroustrup がおそらく正しいと思いますが、私が何を微妙に見逃しているのかわかりません。他の誰かがそれを見つけることができますか?