0
// Overloaded assignment operator for CMessage objects
CMessage& operator=(const CMessage& aMess)
{
    if(this == &aMess) // Check addresses, if equal
    return *this; // return the 1st operand
    // Release memory for 1st operand
    delete[] pmessage;
    pmessage = new char[strlen(aMess.pmessage) + 1];
    // Copy 2nd operand string to 1st
    strcpy_s(this- > pmessage, strlen(aMess.pmessage)+1, aMess.pmessage);
    // Return a reference to 1st operand
    return *this;
}

When using operator overload, the example use reference as a parameter, when comparing if the address is equal, why using & on aMess, why doesn't use if(this == aMess)? Is &aMess an address of reference?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 1
    Because `aMess` is not a pointer, but `this`is? `&aMess`is a pointer to the object referenced by `aMess`. – Deduplicator Apr 22 '14 at 14:31
  • References don't have addresses. By the way, the copy-swap idiom makes `operator=` much simpler, as well as more exception-safe than this. – chris Apr 22 '14 at 14:32

3 Answers3

2

Is &aMess an address of reference?

Yes.

if(this == &aMess) is used to check if they are the same object by comparing their addresses.

if(*this == aMess) is used to check if they are equal by comparing their values.

why doesn't use if(this == aMess)?

if(this == aMess) does not make sense because they are not the same type.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

Yes, this is confusing in C++. In your example, aMess is a reference to an object. Using the name aMess in your code is syntactically the same as using the original object itself, therefore you can not compare it to this which is a pointer. To compare with this, you must convert the reference to a pointer, which is done using the & operator. Just like for a normal object.

EvertW
  • 1,160
  • 9
  • 18
0

When using operator overload, the example use reference as a parameter

This is correct. It wants a reference to a single object, not a (possibly null) pointer to an object or array of objects. Also, it's a const reference, since the function won't be altering the original.

when comparing if the address is equal, why using & on aMess,

Because &aMess is the address of aMess. Pointers with the same address by definition point to the same object.

why doesn't use if(this == aMess)?

That's comparing a pointer (this) to a reference (aMess) and won't compile unless CMessage::operator CMessage* () exists (hopefully it doesn't). If one wanted to compare the values of the objects, they would write if(*this == aMess), which would call bool CMessage::operator ==(const CMessage&) if it exists, or do a binary comparison of the objects if not.

Is &aMess an address of reference?

It's the address of the object. Applying unary & to a reference does the same thing as applying it to the referenced object.


The whole point of this exercise is that the CMessage class has heap-allocated storage in it (pointed to by pmessage) and it could crash if it didn't check if it was copying to itself, because it would delete its storage before copying it.

This function could alternatively have copied the storage first, then changed pmessage to point to the copy, then deleted the old version, which would avoid the need to check for copy-to-self, but would also result in an unnecessary copy in that case.

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96