The Ampersand and other reference/pointer stuff

I began learning c++ about two weeks ago with the online tutorial on cplusplus.com, which seems overall to be the best tutorial I've come across. I have been using NetBeans 6.9 on Lubuntu linux, which in my opinion, greatly enhances learning because of it's code assistance feature, general organization of projects and it's automatic creation and maintenance of makefiles. When you compile a program it will actually change the #include's to include only the ones you need, which is amazing.

http://www.cplusplus.com/doc/tutorial/pointers/ almost covers everything, but I have a question:

On page 98 of the tutorial, under 'keyword this' it says:

int isitme (CDummy& param);


Since the tutorial doesn't cover placing the ampersand after an object, I was wondering if someone could translate this to english for me. I have found it only in the declaration/implementation of an object, like this constructor:

ExampleWindow::ExampleWindow(const ExampleWindow& orig) { }


Is it used in any other way?


type &foo declares a reference, in the tutorial is in Function II ( http://www.cplusplus.com/doc/tutorial/functions2/ )

const type &bar declares a reference that can't modify the object it's referencing, it's used in order not to copy large objects when passing them to some function

& has 2 uses in C++
1. To declare a variable as a reference.
2. To take the address of a variable.

Pointers and references accomplish the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void FuncByValue( int n )
{
  n = 1;
}

void FuncByRef( int& n ) // Example of 1.
{
  n = 2;
}

void FuncByPtr( int *n )
{
  *n = 3;
}

int main()
{
  int i=5;

  FuncByValue( i ); // The value assigned inside the function does not affect i after it returns
  cout << i << endl;

  // In the following 2 function calls, i is modified when the function returns.

  FuncByRef( i ); // same as pointer but compiler automatically takes address for you. 
  cout << i << endl;
 
  FuncByPtr( &i ); // Example of 2.
  cout << i << endl;
}


Output:
5
2
3


Also a 3rd use of & is for bitwise and.
Ok, I see now.
type& foo
is the exact same thing as
type &foo
, or
int& n
is equal to
int &n
. It was the spacing that got me. Stylisticly, which is better?
There are many opinions on that, I personally prefer int &a; because if you write int& a, b; b will look like a reference but it's not, int &a, b; makes it clearer
The spacing always confused me in the beginning, (double &myvar) is much prettier imo.
Most recently, I've tended to keep the pieces of a type together, but I will always choose based on clarity.

Hence, int& a; -- a reference to an int (notice how there is only one variable declared here...)

In practice the only place such things ever bite are in local argument declarations.
1
2
3
4
5
6
int f( int& x, int y )  // no possibility of confusion here...
  {
  int& a;  // for local variables, keep them separate...
  int  b;
  ...
  }
For the local variables, particularly for variables that require initializations, I prefer to keep each to its own line anyway...

As for prettiness, I currently like keeping the baubles with the typenames also...

Relatedly, I like spaces before < and after >, as in: vector <int> & foo;

Hope this helps.
Pointers and references accomplish the same thing.
So why did they add references to C++?
One reason is a pointer does not have to be initialized when declared, whereas a reference does. (A common bug in C is using uninitialized pointers, which usually leads to a crash)

In the previous post the following line will cause a complie error.

int& a;
In the previous post the following line will cause a complie error.


Errr... What?
Yes, a reference must be initialized when declared in a local variable list... (which I did not do in my example). Nice catch!
Topic archived. No new replies allowed.