Solved

What value does & bring to c++

Posted on 2008-06-22
5
168 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 250 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 250 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
How to determine file rename from EventLog ? 2 55
Smart Camera scanning and reading information 3 114
countEvens challenge 2 112
firstChar challenge 13 122
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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