2016-07-05 38 views
1

假设我有一个类A这是定义

class A : virtual SomeOtherClass{ 
    //Stuff here 
}; 

假设我有这样做的地方:

A thing; 
alignas(A) uint8_t arr[sizeof(A)]; 
for (int x = 0; x < sizeof(A); x++) 
{ 
    //Copy into array 
    arr[x] = reinterpret_cast<uint8_t*>(&A)[x]; 
} 

A* otherThing = reinterpret_cast<A*>(arr); 

是我在做什么这里定义的行为,还是我杀我自己好歹我不知道?

+1

'reinterpret_cast'几乎总是一个坏主意。 –

回答

2

所显示的代码执行的东西,这相当于memcpy()

这样,this is undefined behavior。具有虚拟基类的类不是可以复制的。