• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

What value does & bring to c++

Maybe I'm being dense here, but I don't understand what value the & operator brings to the table.  By this I mean that, if I understand its use, then the functionality provided by

int &ii or
CRichEditCtrl &RichCtrl

is little different from int *ii or CRichEditCtrl *RichEdit.

Obviously I am missing something, what is it?

Thanks, Rick
0
rickatseasoft
Asked:
rickatseasoft
  • 2
  • 2
2 Solutions
 
nirusCommented:
'&' is used to create a reference to another variable.

int x = 10;
int& y = x;           // y is a reference

0. A reference will always refer to a variable.
1. No memory gets allocated for y (reference) here. y is an alias of x.
2. A reference cannot be reassigned.to refer to another variable.
3. On displaying x and/or y, both will display the value 10. Value can be changed by changing any one of x and y.

At the same time:
int x = 10;
int *y = &x;         // y is a pointer to x
int *p;                 // p does not point to anything, yet

0. A pointer may not always point to a variable/block of memory
1. A pointer has its own block of memory that contains the address of the pointed variable/block of memory. Doesn't store the value in itself, just the address.
2. A pointer can be reassigned to another variable or block of memory

So - yes, the functionality is different between int *ii  and  int &ii.

Differences between a pointer & a reference are more obvious when we use them as function parameters.

Hope this clarifies.
0
 
rickatseasoftAuthor Commented:
Your answer helps me understand the behavoir a little better, but I still wonder why.  What do these constructs bring to the table that weren't already there?

For instance, I design a lot of generalized functions.  When might I decide to use one of these rather than a pointer?

thanks, rick
0
 
mrwad99Commented:
nirus has given a good overview there.

>> When might I decide to use one of these rather than a pointer?

I feel that you would decide to use a reference as opposed to a pointer because they are safer: consider this:

// .h
void SomeFunctionYouCreate ( CMyClass* pClass )

In the body of this function you access the pClass's members to perform some task, eg

// .cpp
void SomeFunctionYouCreate ( CMyClass* pClass )
{
      int n = pClass->GetInt();
      DoSomethingWithInt();

      // Other uses of pClass
}

Suppose you package this function up into a library.  Later on, a client comes to use this library, and calls

SomeFunctionYouCreate ( NULL );

What will happen?  Boom.  Your library will crash when it tries to access a NULL pointer.  

This is strictly speaking your fault: you should have added to the library

// .h

// User note: pClass cannot be NULL!
void SomeFunctionYouCreate ( CMyClass* pClass )

Given, it is not a lot of work, but it would have been easier to have the function

// .h
void SomeFunctionYouCreate ( CMyClass&  class )

Then the user cannot pass NULL.  Given, they could pass an alias for an object that has since been deleted or gone out of scope, but then that is *their* fault.  A function taking a reference is much harder to sabotage in this way than a function taking a pointer.

HTH
0
 
rickatseasoftAuthor Commented:
Thanks
0
 
mrwad99Commented:
Glad to help.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now