A Question On Reference Type

This is a discussion on A Question On Reference Type within the C++ Programming forums, part of the General Programming Boards category; First please look at the code Code: #include <iostream> using namespace std; class Test { public: Test() { cout << ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    A Question On Reference Type

    First please look at the code

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Test {
    public:
        Test() { cout << "Test" << endl; }
        ~Test() { cout << "~Test" << endl; }
    };
    
    int main()
    {
        const Test& t = Test();
        //Test();
        cout << "haha" << endl;
    
        return 0;
    }
    and the output

    Test
    haha
    ~Test
    If I comment out "const Test& t = Test();" and use "Test();" instead, the output will be

    Test
    ~Test
    haha
    Don't you think it weird? Does the standard say that a temporary object's destruction will be deferred if it is referenced by an object of const reference type?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    what this has to do with const?
    first sample creates object that is destructed when the function main is finished

    second sample creates temporary object that is destructed as soon as the call to Test() is finished...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by vart View Post
    what this has to do with const?
    first sample creates object that is destructed when the function main is finished

    second sample creates temporary object that is destructed as soon as the call to Test() is finished...
    but why the temporary object in the first sample is destroyed when after main is finished where as the temporary object in the second sample is destroyed immediately? Test() creates just the same temporary object no matter in the 1st/2nd sample, the only difference is that in the 1st sample, Test() is referenced

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    In the first example, the standard guarantees the object will exist until the enclosing scope is exited - ie on return from main().

    In the second example, the only guarantee is that the temporary object will exist until the next sequence point - which means it may be destroyed before the next statement. That next statement is your cout << "haha" line.....

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by grumpy View Post
    In the first example, the standard guarantees the object will exist until the enclosing scope is exited - ie on return from main().

    In the second example, the only guarantee is that the temporary object will exist until the next sequence point - which means it may be destroyed before the next statement. That next statement is your cout << "haha" line.....
    Can you please tell me which section of the Standard guarantees these behaviors and why the 1st behaviour is guaranteed? For what good?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Antigloss View Post
    Can you please tell me which section of the Standard guarantees these behaviors and why the 1st behaviour is guaranteed? For what good?
    As long as you have a variable that refers to the object, the object must also exist. Since the variable t exists within the entire part of main, the object must also exist that long. In the second case, you create an object with nothing to "hold on to" the object, so there is no need for it to exist more than the blink that is from it's construction to it's destruction - how is the object going to be used if there's nothing to refer to it by?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's 12.2/5 in the current draft standard for C++0x. I don't know where it is in the original, but it's there.

    As for why, I don't know; there's no rationale in the standard. Probably for this:
    Code:
    const T &ref = foo();
    This code should work whether foo() returns an object or a reference.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    well, i see. Thanks alot, everybody

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    By coincidence, it's in Section 12.2 para 5 of the 1998 standard as well.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Would you really want the object to be destructed before you're finished using it?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> By coincidence, it's in Section 12.2 para 5 of the 1998 standard as well.
    I doubt that's coincidence.

    >> Would you really want the object to be destructed before you're finished using it?
    No, but that happens if you use a non-const reference or a pointer, so tat rationale doesn't seem to apply here.

  12. #12
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Probably for this:
    Code:
    const T &ref = foo();
    In the new standard, this will be possible with an rvalue reference if 'foo()' is just a function? The code CornedBeef showed is erroneous for the current standard? The line
    Code:
    const Test& t = Test();
    does work because this creates the temporary object.

    Am I right?
    Last edited by MarkZWEERS; 08-12-2008 at 12:52 PM.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Huh?

    The code is perfectly valid for the current standard. If the function returns by-value, the reference will bind to the return value temporary, whose lifetime is extended. If the function returns by-ref, the reference will alias the return value referenced object, and the lifetime must be long enough anyway.

    Rvalue references bring a new dimension to the game. I don't actually know what happens if you try to make a const lvalue reference alias an rvalue reference.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Sorry, I've got it a little wrong.

    It is due to the 'const' declaration that the reference can bind to a temporary in
    Code:
    const Test& t = Test();
    or in
    Code:
    const T &ref = foo();
    Non-const references cannot be bind to temporaries=rvalues , so there's where the rvalue reference comes in place:
    Code:
    Test&& t = Test();
    int&& t = foo(); // suppose foo returns an int
    I'll have to study a little more on move semantics, but from what I've seen I love it :-)

    http://www.artima.com/cppsource/rvalue.html

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Is
    Code:
    Test t(); //or 
    Test t;
    Equal to
    Code:
    Test &t =Test();
    ?
    Last edited by siavoshkc; 08-14-2008 at 01:21 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 04:15 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 10:45 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 06:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21