Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reference Variable

Posted on 1998-08-16
3
Medium Priority
?
180 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 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.
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

972 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