11

I am kind confused about this case:

Declare a pointer:

int b =10;
int*a=&b;

Here & takes the address of b.

Consider another example:

/* Reference to the calling object can be returned */

Test& Test::func ()
{
   // Some processing
   return *this;
} 

this should be a pointer, *this is the obeject pointed. But here we are asking to assign *this to &Test.

What should we modify the code to let the function return the address. Should we still use Test& ?

Weijia
  • 272
  • 1
  • 5
  • 12
  • Make it return a pointer. – Weak to Enuma Elish Feb 24 '16 at 06:01
  • 3
    Pay attention on position of `&`: in `Test&` it is on right, but in `&b` it is on left. Also important is that `Test` is a type, but `b` is an object/variable. – VolAnd Feb 24 '16 at 06:10
  • There are only so many keys on a keyboard, and the letters and digits already have other uses. On an unrelated note, there's no such thing as a "reference operator". `operator` in C++ has a specific meaning, it applies to expressions not types. – MSalters Feb 24 '16 at 08:42
  • @VolAnd While a nice observation, this isn't a good rule to follow as this is completely arbitrary. My favorite example is `f(int *&a)` or f(int*& a)`. What is this? One might never know. – hgiesel Feb 24 '16 at 10:46
  • @henrikgiesel This is definitely `f(int*& a)`, i.e. pointer that is passed by reference to function `f()`. It is a reasonable alternative to using pointer-to-pointer (i.e. `f(int** a)`) for functions that allocate memory in body (or change address for any other reason) – VolAnd Feb 24 '16 at 12:03

3 Answers3

16

In C++ there're two different syntax units:

&variable; // extracts address of variable

and

Type& ref = variable; // creates reference called ref to variable

Easy usage example:

int v = 5;

cout << v << endl; // prints 5
cout << &v << endl; // prints address of v

int* p;
p = &v; // stores address of v into p (p is a pointer to int)

int& r = v;

cout << r << endl; // prints 5

r = 6;

cout << r << endl; // prints 6
cout << v << endl; // prints 6 too because r is a reference to v

As for using references in functions, you should google "passing by reference in C++", there're many tutorials about it.

Fomalhaut
  • 8,590
  • 8
  • 51
  • 95
3

Firstly, this is a pointer. The * dereferences the pointer, meaning return *this; returns the object, not a pointer to it.

Secondly, Test& is returning a reference to a Test instance. In your case, it is a reference to the object. To make it return a pointer, it should be Test*.

It makes more sense if you read a pointer declaration from right to left.

Test* func(); //When I call func, and dereference the returned value, it will be a Test
Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36
3

But here we are asking to assign *this to &Test.

No... you're asking for the value/expression *this to be used to return a Test&, which is a reference to a Test object. What that does is return a reference to the object on which func() is invoked.

What should we modify the code to let the function return the address. Should we still use Test& ?

You should use Test* instead... pointers are addresses, and having changed the return type you could return this (which is a pointer), but not *this because *this is not a pointer.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252