Solved

Reference Variable

Posted on 1998-08-16
3
175 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

685 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