-1

Here is the code which basically implementing the = assignment for a class named CMyString, and the code is right.

CMyString& CMyString::operator =(const CMyString &str) { 
if(this == &str) 
    return *this; 

delete []m_pData; 
m_pData = NULL; 

m_pData = new char[strlen(str.m_pData) + 1]; 
strcpy(m_pData, str.m_pData); 

return *this; 
} 

The instance is passed by reference, and the first 'if' is checking whether the instance passed in is itself or not. My question is: why does it use &str to compare, doesn't str already contain the address of the instance? Could any one explain how this line works?

Also, I just want to make sure that this contains the address of the object: Is this correct?

didierc
  • 14,572
  • 3
  • 32
  • 52
fiftyplus
  • 561
  • 10
  • 18
  • A suggestion, Inorder to make assignment operation execption safty Its better to store m_pData in a temp pointer delete temp pointer at the end so that even exception happens in allocating memory for m_pData = new char[strlen(str.m_pData) + 1]; the source object data is safe CMyString& CMyString::operator =(const CMyString &str) { if(this == &str) return *this; char *temp = m_pData; m_pData = new char[strlen(str.m_pData) + 1]; strcpy(m_pData, str.m_pData); delete [] temp; temp = NULL; return *this; } – shivakumar Apr 09 '13 at 06:11
  • I know that, I was thinking about put something like functional but without exception handling, but that's not really what i am concerning here...but thanks anyway – fiftyplus Apr 09 '13 at 06:14

4 Answers4

4

isn't str already contains the address of the instance

No. A reference is the object itself. It's not a pointer to the object.

(I. e., in the declaration of the function, &str stands for "reference to str" and not "address of str" - what you're talking about would be right if the function was declared like this:

CMyString& CMyString::operator =(const CMyString *str);

but it isn't.)

  • just found out there is a difference about pass by reference between C and C++, In C, reference is passed by use &a, and a same type pointer will be used to access the value stored in this pointer. But seems like in C++, no need to use &a, it can just use a, and parameter must be a reference type. which in my case is (CMyString &str). so when a str object is passed in, what exactly this reference type mean? is that just as same as the original one? seems like this reference type object's usage looks very similar to a value type object. THanks – fiftyplus Apr 09 '13 at 07:17
  • 2
    @fifty C doesn't have references. C has pointers. When you use foo(&a) in C, you pass a pointer to a, and signature of foo is foo(type_of_a*). In C++ you can do the same thing, plus pass-by-reference. Signature of foo becomes foo(type_of_a&), and you use it as foo(a). Here a is not a pointer, it's a reference. As H2CO3 said, a reference is the object itself. You use this reference as you would use the object. See answer of Jermaine Xu on operator&, which might explain your confusion. – undu Apr 09 '13 at 08:01
1

Address-of Operator and Reference Operator are different.

The & is used in C++ as a reference declarator in addition to being the address-of operator. The meanings are not identical.

int target;
int &rTarg = target;  // rTarg is a reference to an integer.
                      // The reference is initialized to refer to target.
void f(int*& p);      // p is a reference to a pointer

If you take the address of a reference, it returns the address of its target. Using the previous declarations, &rTarg is the same memory address as &target.

StarPinkER
  • 14,081
  • 7
  • 55
  • 81
0

str passed by to the assignment operator is passed by reference, so it contains the actual object, not its address. A this is a pointer to the class a method is being called on, so if one wants to compare, whether passed object is the same object itself, he has to get the address of str in order to compare.

Note, that & behaves differently, depending on where it is used. If in statement, it means getting an address to the object it is applied to. On the other hand, if it is used in a declaration, it means, that the declared object is a reference.

Consider the following example:

int i = 42;
int & refToI = i; // A reference to i
refToI = 99;
std::cout << i; // Will print 99

int j = 42;
int * pJ = &j; // A pointer to j
*pJ = 99;
std::cout << j; // Will print 99

this is a pointer to the instance, so yes, it contains the address.

The whole point of verifying, if the passed object is this or not is to avoid unnecessary (or, possibly destructive) assignment to self.

Spook
  • 25,318
  • 18
  • 90
  • 167
0

While indeed a variable reference - denoted by the symbol & after the type name - underlying implementation is usually a pointer, the C++ standard seemingly does not specify it.

In its usage anyway, at the syntax level, a reference is used like a non referenced value of the same type, ie. more strictly speaking :

If the type of the variable is T &, then it shall be used as if it were of type T.

If you must write str.someMethod() and not str->someMethod() (without any overloading of the arrow operator), then you must use & to obtain the address of the value. In other words, a reference acts more or less like an alias of a variable, not like a pointer.

For more information about references and pointers, see these questions:

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52