1

I was confused by this piece of code I recently saw on stackoverflow. The return value must be IntArray& (so an address, right?). But then he returns *this (a value, right?). This seems not logic to me.. This question could be a duplicate but i didn't found it so very sorry if it is. Here is the code:

IntArray& IntArray::operator=(const IntArray& other){
     if(this == &other){
         return *this;
     }
     ...
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
GNIUS
  • 145
  • 1
  • 11
  • 1
    Read about *references* in C++. The `&` in reference syntax has absolutely no relation to "address-of" operator and/or pointers. No, it does not mean that the return value is an address. References is a very broad topic, which cannot be taught in single a SO answer. Open you favorite C++ book. – AnT stands with Russia Aug 11 '15 at 16:37
  • First step - learn the language. – Lightness Races in Orbit Aug 11 '15 at 17:14

1 Answers1

2

Your confusion is all due to IntArray&, which is not an address, it's a reference.

While it is true that operator& (unless overloaded) usually yields a pointers in an expression like &value, in this case & in any generatic T& (for some type T) has nothing to do with that operator, it's associated with the type.

In a similar way, T&& is an rvalue reference to T.

In your specific instance, then:

IntArray& IntArray::operator=(const IntArray& other){
     if(this == &other){
         return *this;
     }
     ...
}

you have that this is a pointer to IntArray, and by dereferencing it with operator* you are obtaining an IntArray& value, which matched perfectly with the return type of that function.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • but I learned on college that if you make a reference ex. 'int &ex = d;' then '&ex' will give the adress of d and 'ex' will give the value.. – GNIUS Aug 11 '15 at 16:17
  • @GNIUS Either you are mistaken with `int* ex = &d;` or your college tought you wrong. In `int& ex = d;` you are *aliasing* `d` with the reference `ex`. – Shoe Aug 11 '15 at 16:18
  • @GNIUS [cppreference page about references](http://en.cppreference.com/w/cpp/language/reference) also the usage of `&` inside a *declaration* is different than its usage ([operator&](http://en.cppreference.com/w/cpp/language/operator_member_access)) inside an *expression* (and jeffrey is right) – Caninonos Aug 11 '15 at 16:19
  • now your pointing to a reference? ex points to the value where d reference? i don't get it. I thought i finally understood everything :( – GNIUS Aug 11 '15 at 16:22
  • @GNIUS `int* ex = &d;` is not a pointer to a reference. `int* ex = &d;` means create a pointer to an int called ex (`int* ex`) and set it to the address of (`&`) d. `&` in c++ is used for references and it is the [address of](https://msdn.microsoft.com/en-us/library/64sa8b1e.aspx) operator – NathanOliver Aug 11 '15 at 16:26
  • @NathanOliver, i undestand. In this site (http://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/) they return just value when &object is expected? is it fault to return &this? – GNIUS Aug 11 '15 at 17:00
  • @GNIUS: Your college taught you wrong. Or, rather, it misled you. It seems your professor was hung up on antiquated "declaration mirrors use" comprehension tricks that just don't work in real life, certainly not in this century. If you make a reference, e.g. `int& ex = d`, then `ex` is a reference. And using it gives you the value of the referee. `&ex` gives you the address of `d` but this meaning of `&` is completely unrelated. – Lightness Races in Orbit Aug 11 '15 at 17:15
  • @LightnessRacesinOrbit what is the meaning of & here? After reading/learning i think : it means it is a reference to an IntArray object. if you return *this, it returns the value where *this to referee. Is this right? – GNIUS Aug 11 '15 at 17:37
  • @GNIUS I would recommend starting from [here](http://stackoverflow.com/questions/31946885/pointers-c-ask-adress-gives-value/31946902?noredirect=1#comment51805595_31946885). Learn the language again, maybe via some of [these awesome books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). When `&` is used near a type, it's usually a reference, when it's used in front of a value, then it's `operator&` (which is the "take the address" operator). I cannot explain it better than this, sorry. – Shoe Aug 11 '15 at 17:41
  • @GNIUS: `*this` is actually not a reference :) – Lightness Races in Orbit Aug 12 '15 at 11:59