1

Suppose I have a class A:

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

Suppose I have do this somewhere:

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);

Is what I am doing here defined behavior, or am I killing myself in someway that I am unaware?

DarthRubik
  • 3,927
  • 1
  • 18
  • 54

1 Answers1

2

The shown code executes something that's equivalent to memcpy().

As such, this is undefined behavior. Classes with virtual base classes are not trivially-copyable.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148