Why can't static_cast be used to down-cast when virtual inheritance is involved?

后端 未结 6 794
情深已故
情深已故 2020-11-28 07:37

Consider the following code:

struct Base {};
struct Derived : public virtual Base {};

void f()
{
    Base* b = new Derived;
    Derived* d = static_cast<         


        
6条回答
  •  情话喂你
    2020-11-28 07:48

    I suppose, this is due to classes with virtual inheritance having different memory layout. The parent has to be shared between children, therefore only one of them could be laid out continuously. That means, you are not guaranteed to be able to separate a continuous area of memory to treat it as a derived object.

提交回复
热议问题