Solved

Reference Variable

Posted on 1998-08-16
3
172 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
Comment Utility
Edited text of question
0
 
LVL 1

Accepted Solution

by:
willemnel earned 10 total points
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

6 Experts available now in Live!

Get 1:1 Help Now