Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reference Variable

Posted on 1998-08-16
3
Medium Priority
?
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Author Comment

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

Accepted Solution

by:
willemnel earned 20 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

718 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