Solved

Reference Variable

Posted on 1998-08-16
3
173 Views
Last Modified: 2010-04-01
Hello freinds,
                    my doubt is why we can't declare a reference variable for a  void pointer.
Please clarify my doubt.Thanks in advance.
 
0
Comment
Question by:66
3 Comments
 

Author Comment

by:66
ID: 1170445
Edited text of question
0
 
LVL 1

Accepted Solution

by:
willemnel earned 10 total points
ID: 1170446
I hope I've understood your'e question correctly. Anyway, here goes:

In C++, a reference is a form of pointer that can be used to access a variable by its address. Also, when a reference variable is declared, it MUST reference (point to) something. For example:

int a;
int &b=a; // Now b references a (points to a)

a=5; // a AND b will now have the value 5

By definition a reference must point to SOMETHING, while a void pointer points to NOTHING. Hence you can see that what your'e trying to do is a contradiction.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170447
Also, unlike a pointer, a reference must always point to the same thing.  You can change a pointer to point to different things.  But a reference cannot be made to point to (refer to) different things, for example, you can do

int i;
int j;
int *IPtr = &i;
IPtr = &j; // Make pointer point to other int.

Byut if you do.

int i;
int j;
int IRef = i;   // IRef refers to forever.
IRef = j;   // This changes the value in what IRef refers to (i).  IRef still refers to i.

you don't change IRef to refer to something else.   It still refers to i, but now i had a new value.  Thus, there is less reason to have NULL references since you can't start out with a NULL reference and then change it to something else later.
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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

919 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

18 Experts available now in Live!

Get 1:1 Help Now