Solved

What value does & bring to c++

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 52
bigDiff challenge 17 84
Is there a simple front-end menu system. 9 71
Message not shown 5 33
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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

867 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

20 Experts available now in Live!

Get 1:1 Help Now