Implementing the assignment operator, also checking for self-assignment reported...
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sun, 4 Sep 2011 11:59:20 +0000 (08:59 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sun, 4 Sep 2011 12:05:47 +0000 (09:05 -0300)
- Warning 1529: Symbol 'MessagePayload::operator=(const MessagePayload &)' not first checking for assignment to this
- Warning 1539: member 'MessagePayload::PayloadSizeInBytes' not assigned by assignment operator
- Warning 1539: member 'MessagePayload::Payload' not assigned by assignment operator

src/host/messagepayload.cpp

index 801a9f8..4d10a1b 100644 (file)
@@ -76,9 +76,17 @@ MessagePayload& MessagePayload::operator=( const MessagePayload &other )
     BOOST_ASSERT( Payload.get() != NULL );
     BOOST_ASSERT( other.Payload.get() != NULL );
 
-    // FIXME must resize the Payload in order to work
+    // checking for self-assignment (i.e. object1 = object1)
+    if ( &other == this ) {
+        return *this;
+    }
 
-    copy( other.Payload.get(), other.Payload.get() + PayloadSizeInBytes, Payload.get() );
+    size_t new_payload_size_in_bytes = other.PayloadSizeInBytes;
+    uint8_t *new_payload_data = new uint8_t[ new_payload_size_in_bytes ];
+    Payload.reset( new_payload_data );
+    PayloadSizeInBytes = new_payload_size_in_bytes;
+
+    copy( other.Payload.get(), other.Payload.get() + other.PayloadSizeInBytes, Payload.get() );
 
     BOOST_ASSERT( PayloadSizeInBytes == other.PayloadSizeInBytes );