CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Question about local variable used as a reference.

    Hi, everyone!


    I have only noticed that reference variable are used as
    parameter of a function before (or return value). For
    example,


    Source Code:
    --------
    function (const T& t)
    --------


    But today when reading the source code of some other's
    today. I noticed that a local variable is declared as
    reference type. Before I only noticed that lcoal variable
    are used as pointer to origin type and origin type.

    Source Code:
    --------
    T *pt;
    T t;
    --------

    But now I noticed that a variable is declard like this in a
    function,


    Source Code:
    --------
    T t1;
    T & t2 = t1;
    --------


    I want to know if a reference is used as a local variable, are there
    any special things to notice? Or simply just the same as the reference
    variable of a function parameter?


    Thanks in advance,
    George

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Question about local variable used as a reference.

    Originally posted by George2
    Source Code:
    --------
    T t1;
    T & t2 = t1;
    --------
    Yes, you are right. Declaring a reference as a local variable has the same effect as a reference variable of a function parameter. It goes out-of-scope when the function ends.

    Also after the reference has been initialized, you can't re-reference it to a different variable.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking Re: Re: Question about local variable used as a reference.

    Thanks, Kheun buddy!

    George

    Originally posted by Kheun

    Yes, you are right. Declaring a reference as a local variable has the same effect as a reference variable of a function parameter. It goes out-of-scope when the function ends.

    Also after the reference has been initialized, you can't re-reference it to a different variable.

  4. #4
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Correction...

    Also after the reference has been initialized, you can't re-reference it to a different variable.
    That's not true. t2 in your example can be reassigned to reference different instances of T as often as you like. You need a different declaration to prevent t2 fro being reassined:

    Code:
    T& const t3  = t1;
    t3 is a variable that can only be assigned once.

    - Kevin

    P.S. There is a FAQ related to this. (http://www.codeguru.com/forum/showth...hreadid=231042)

  5. #5
    Join Date
    Aug 2002
    Posts
    58
    t2 in your example can be reassigned to reference different instances of T as often as you like.
    No, it can't. An object reference cannot be changed to reference another object.

    Code:
    T t1, t4;
    T & t2 = t1; // assigns the reference(alias) 't2' for 't1'
    t2 = t4; // invokes the assignment operator for t1
    The const declaration you provided prevents modification of t1 through t3.

    http://www.parashift.com/c++-faq-lite/references.html

    See especially section 8.5.

    Regards,
    Bassman

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Bassman's correct - you cannot reseat references. Also, the following is illegal:
    Code:
    T t1;
    T& t2;    // Illegal - reference must be initialised
    
    t2 = t1; // Also illegal, can't reseat a reference
              // (or does it mean call T::operator=()?)
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Interesting.... I can't believe I have misundestood this for so long!

    Thanks for clearing this up for me,

    Kevin

  8. #8
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    (now deeply embarrased...) I should have realized that earlier -- much of the code I worked with and written uses that fact that operator=() gets called. As soon as I typed an example, I realized how wrong I was. I don't know why I held on to that belief. Oh well.

    - Kevin

  9. #9
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    Yes I agree, KevinHall buddy!

    When sit down and think the very basic idea of
    C++ prgramming. I also find myself a child of this
    area even finished several projects.


    regards,
    George

    Originally posted by KevinHall
    (now deeply embarrased...) I should have realized that earlier -- much of the code I worked with and written uses that fact that operator=() gets called. As soon as I typed an example, I realized how wrong I was. I don't know why I held on to that belief. Oh well.

    - Kevin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured