reference variable and local variable

Discussion in 'C++' started by Kench, Jun 28, 2004.

  1. Kench

    Kench Guest

    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?
    Kench, Jun 28, 2004
    #1
    1. Advertising

  2. Kench

    Howard Guest

    "Kench" <> wrote in message
    news:cbphgb$...
    > 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
    Howard, Jun 28, 2004
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Jun 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Hoffmann
    Replies:
    3
    Views:
    2,803
    Christian Jan├čen
    Aug 8, 2003
  2. romerun
    Replies:
    6
    Views:
    14,724
    Alf P. Steinbach
    Sep 3, 2004
  3. George2
    Replies:
    15
    Views:
    658
    James Kanze
    Dec 17, 2007
  4. Replies:
    9
    Views:
    462
    Ron Natalie
    Dec 30, 2007
  5. Replies:
    2
    Views:
    469
    Jim Langston
    May 30, 2008
Loading...

Share This Page