By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,542 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,542 IT Pros & Developers. It's quick & easy.

reference variable and local variable

P: n/a
I was curious and playing with pointers and references to see what's
different between them.
Other than the obvious ones involving C++ syntax & things like references
cannot be modified with addition & subtraction,
pointers can be modified, both are similar.
It stated me going what are the diferrence between a reference vairable and
local variable ?
Here are what I can think of
1. For a local variable constructor is called, not the case for a reference
variable.
2. Local variable cannot be NULL. Reference variable can point to a NULL
object by a simple trick like. Employee* e = NULL; Employee* ref = *e;
Anything else?

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a

"Kench" <no**@none.com> wrote in message
news:cb*********@kcweb01.netnews.att.com...
I was curious and playing with pointers and references to see what's
different between them.
Other than the obvious ones involving C++ syntax & things like references
cannot be modified with addition & subtraction,
pointers can be modified, both are similar.
It stated me going what are the diferrence between a reference vairable and local variable ?
Here are what I can think of
1. For a local variable constructor is called, not the case for a reference variable.
2. Local variable cannot be NULL. Reference variable can point to a NULL
object by a simple trick like. Employee* e = NULL; Employee* ref = *e;
Anything else?


I'm puzzled why you're using the term "local" at all. The term "local
variable" indicates a variable declared in local scope, i.e., inside a
function. That has nothing to do with the type of variable (i.e., built-in,
user-defined, pointer, or reference).

Also, you refer to a reference, but your example (Empoyee*) is a pointer.
Which did you mean?

Given these discrepencies, I won't comment on your points, because they
don't make sense without my making assumptions about what I *think* you
meant. Your best bet is to read a good C++ book. There should be plenty of
information about pointer-type variables and reference-type variables. Then
come back here and we'd be glad to help clear up any specific points, once
you have the terminology correct so we know what you mean.

-Howard


Jul 22 '05 #2

P: n/a
Kench wrote:
I was curious and playing with pointers and references to see what's
different between them.
Other than the obvious ones involving C++ syntax & things like references
cannot be modified with addition & subtraction,
pointers can be modified, both are similar.
"both are similar" is an incomplete statement. Both are similar
to what? If you wanted to state that they are similar, you should
not have used the word "both".
It stated me going what are the diferrence between a reference vairable and
local variable ?
A "reference variable" is what is known as "an alias", a different
way to refer (lacking a better word) to an object.
Here are what I can think of
1. For a local variable constructor is called, not the case for a reference
variable.
The constructor is called for an object, not for a variable.
2. Local variable cannot be NULL.
Only pointers can be NULL.
Reference variable can point to a NULL
object by a simple trick like. Employee* e = NULL; Employee* ref = *e;
This is not a "trick", it is a well known way to cause _undefined_
behaviour. Dereferencing a NULL pointer is _not_ allowed. So, in
a valid C++ program there can be no "NULL" references.
Anything else?


A variable can have a type "reference to T", where T designates another
type. There are special restrictions to what T could be. However,
nothing precludes a variable from being a reference and local at the
same time.

Victor
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.