reference variable and local variable

K

Kench

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?
 
H

Howard

Kench said:
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
 
V

Victor Bazarov

Kench said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top