Reference Variable as an Alias... Huh?

Discussion in 'C++' started by mexican_equivalent@yahoo.com, Oct 4, 2005.

  1. Guest

    X-no-archive: yes


    Newbie C++ programmer reads the following statement from the C++
    'Deitel & Deitel' textbook:

    "Reference variables can be used as local aliases within a function.
    They must be initialized upon declaration, and cannot be reassigned to
    other variables thereafter. All operations supposedly performed on the
    alias is actually performed on the variable."

    So my question is... what the heck is the point of using a reference
    variables as an alias? It seems so much simpler to use the variable
    itself instead of using an alias. I don't get it.
     
    , Oct 4, 2005
    #1
    1. Advertising

  2. Guest

    Using the reference variable allows you to change the value of the
    variable in your function. If you don't pass it by reference, then a
    copy of the variable will be created in your function (pass by value).
    Answering your question, aliases are for speed and for modifying
    variables in functions.
     
    , Oct 4, 2005
    #2
    1. Advertising

  3. Mike Austin Guest

    wrote:
    > X-no-archive: yes
    >
    >
    > Newbie C++ programmer reads the following statement from the C++
    > 'Deitel & Deitel' textbook:
    >
    > "Reference variables can be used as local aliases within a function.
    > They must be initialized upon declaration, and cannot be reassigned to
    > other variables thereafter. All operations supposedly performed on the
    > alias is actually performed on the variable."
    >
    > So my question is... what the heck is the point of using a reference
    > variables as an alias? It seems so much simpler to use the variable
    > itself instead of using an alias. I don't get it.


    Think about passing a reference argument to a function:

    void setTo10( int& i ) { i = 10; }

    If you didn't use a reference, you would be setting a *copy* of what you passed
    to setTo10(). Another way to write this function would be with a pointer:

    void setTo10( int* i ) { *i = 10; }


    Mike
     
    Mike Austin, Oct 4, 2005
    #3
  4. Ian Guest

    wrote:
    > X-no-archive: yes
    >
    >
    > Newbie C++ programmer reads the following statement from the C++
    > 'Deitel & Deitel' textbook:
    >
    > "Reference variables can be used as local aliases within a function.
    > They must be initialized upon declaration, and cannot be reassigned to
    > other variables thereafter. All operations supposedly performed on the
    > alias is actually performed on the variable."
    >
    > So my question is... what the heck is the point of using a reference
    > variables as an alias? It seems so much simpler to use the variable
    > itself instead of using an alias. I don't get it.
    >

    Very handy shorthand if you want to work on a nested struct member, for
    example the value part of a std::map iterator.

    int& n = thing.bit.int;

    or

    Thing& thing = *iterator.second;


    Ian
     
    Ian, Oct 4, 2005
    #4
  5. In message <>,
    writes

    [no context quoted - please learn how to quote when using Google]

    >Using the reference variable allows you to change the value of the
    >variable in your function. If you don't pass it by reference, then a
    >copy of the variable will be created in your function (pass by value).


    You're talking about passing *parameters* by reference. The OP is asking
    about creating a local (automatic) reference to the result of some
    expression.

    >Answering your question, aliases are for speed and for modifying
    >variables in functions.


    Answering his actual question, references can be used as shorthand to
    avoid repeatedly having to type a complex expression, e.g. one involving
    function calls or the . -> or [] operators..

    --
    Richard Herring
     
    Richard Herring, Oct 4, 2005
    #5
    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. Jim Owen

    Output parameters undefined? Huh?

    Jim Owen, Oct 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    433
    Rajesh.V
    Oct 22, 2003
  2. VB Programmer
    Replies:
    1
    Views:
    362
    Chris Moore
    Jun 12, 2004
  3. some dude somewhere

    doc types! huh?

    some dude somewhere, Jan 3, 2004, in forum: HTML
    Replies:
    6
    Views:
    453
    Sam Hughes
    Jan 3, 2004
  4. grocery_stocker
    Replies:
    9
    Views:
    816
    grocery_stocker
    May 24, 2008
  5. Les Juby
    Replies:
    2
    Views:
    125
    Aaron Bertrand - MVP
    Aug 29, 2003
Loading...

Share This Page