Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What value does & bring to c++

Posted on 2008-06-22
5
Medium Priority
?
172 Views
Last Modified: 2013-11-20
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
Comment
Question by:rickatseasoft
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
nirus earned 1000 total points
ID: 21844322
'&' 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
 

Author Comment

by:rickatseasoft
ID: 21854523
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
 
LVL 19

Assisted Solution

by:mrwad99
mrwad99 earned 1000 total points
ID: 21865654
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
 

Author Closing Comment

by:rickatseasoft
ID: 31469605
Thanks
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 21952832
Glad to help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question