这是定义的

时间:2016-07-05 01:00:44

标签: c++ reinterpret-cast type-punning

假设我有一个班级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 个答案:

答案 0 :(得分:2)

显示的代码执行的内容相当于memcpy()

因此,this is undefined behavior。具有虚拟基类的类不是简单的可复制的。

相关问题