Solved

What value does & bring to c++

Posted on 2008-06-22
5
163 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now