What is the difference between bitwise and memberwise copying? Surely if you copy the members then you will end up copying the bits representing the members anyway?
class MyClass
{
public:
MyClass () : m_p (new int (5)) {}
~MyClass () {delete m_p;}
int* m_p;
};
MyClass a;
MyClass b;
memcpy (&a, &b, sizeof (a));
I just leaked the allocated int in 'a' by rewriting it's member variable without freeing it first. And now 'a' and 'b' have an m_p that is pointing to the same memory location and both will delete that address on destruction. The second attempt to delete that memory will crash.
Both are the same, so that the entire object is trivially copyable, if all subobjects are trivially copyable. (Class (sub)objects also must not have virtual member functions or virtual base classes.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With