469,906 Members | 2,224 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,906 developers. It's quick & easy.

reference variable and local variable

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
2 2415

"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
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.

Similar topics

7 posts views Thread by Pekus Cons. e Desenvolvimento | last post: by
5 posts views Thread by David++ | last post: by
11 posts views Thread by ravi | last post: by
10 posts views Thread by Robert Dailey | last post: by
6 posts views Thread by lisp9000 | last post: by
7 posts views Thread by pauldepstein | last post: by
12 posts views Thread by Bryan Parkoff | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.