C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

reference variable and local variable

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Kench
Guest





PostPosted: Mon Jun 28, 2004 4:48 pm    Post subject: reference variable and local variable Reply with quote



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?



Back to top
Howard
Guest





PostPosted: Mon Jun 28, 2004 5:07 pm    Post subject: Re: reference variable and local variable Reply with quote




"Kench" <none (AT) none (DOT) com> wrote

Quote:
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







Back to top
Victor Bazarov
Guest





PostPosted: Mon Jun 28, 2004 5:49 pm    Post subject: Re: reference variable and local variable Reply with quote



Kench wrote:
Quote:
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".

Quote:
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.

Quote:
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.

Quote:
2. Local variable cannot be NULL.

Only pointers can be NULL.

Quote:
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.

Quote:
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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.