3

I have a structure of the following type:

struct SPacket
{
    unsigned char payload[260]; 
    unsigned int payloadLength; 
}; 

I have a pointer object to this structure:

SPacket* ptrObj; 

how can I perform a deep copy of ptrObj into a another object:

SPacket obj;  
Paindoo
  • 173
  • 3
  • 8
  • Possible duplicate of [pointers - duplicate object instance](http://stackoverflow.com/questions/14050058/pointers-duplicate-object-instance) – Khalil Khalaf Aug 11 '16 at 13:56

2 Answers2

8

The compiler-generated copy constructor will deep-copy the array member, so just use it:

SPacket obj(*ptrObj);
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
3

The solution is extremely simple: use the copy constructor:

SPacket obj = *ptrObj;

This will call the (implicit) copy constructor.

bolov
  • 72,283
  • 15
  • 145
  • 224