How C++ reference works

前端 未结 5 1130
别那么骄傲
别那么骄傲 2020-12-17 05:54

After working 15 years in C++ I found that I don\'t understand references completely...

class TestClass
{
public:
    TestClass() : m_nData(0)
    {
    }

    T         


        
相关标签:
5条回答
  • 2020-12-17 06:35

    Your code suffers from multiple problems and ultimately won't make sense. However, let's hack through it.

    1) You can only bind a temporary to a const reference, thus extending its lifetime:

    const TestClass & c = TestClass();
    

    2) Now we can't use dump, because you didn't declare it const:

    void Dump() const
    

    3) Saying c = TestClass() is an assignment. However, c is now a reference-to-const, which cannot be assigned to, since assignment is non-constant (for obvious reasons). Let's hack around this:

    const_cast<TestClass&>(c) = TestClass(10);
    

    Now we've assigned a new value to the temporary-but-extended object c, and all is as it should be:

    main started
    data = 0  ptr = 0x0xbfa8219c
    destructor
    data = 10  ptr = 0x0xbfa8219c
    main ended
    destructor
    

    The pointers are the same because there's only one object, namely the (temporary) one referenced by c. Assigning to it is a hack that's undefined behaviour in general, but we get away with it for the purpose of this demonstration.

    The intermediate destructor is that of the second temporary TestClass(10).

    0 讨论(0)
  • 2020-12-17 06:37
    TestClass& c = TestClass(); // TestClass() temporary doesn't persist beyond this expression.
    c.Dump();
    

    TestClass() creates a temporary and you cannot take the reference of it.

    const TestClass& c = TestClass();
    

    const qualification extends the life time of the temporary being created until the scope of the object c.

    0 讨论(0)
  • 2020-12-17 06:41
    TestClass& c = TestClass();
    

    This wouldn't even compile!

    Attempting to bind a temporary to non-const reference would result in compilation error.

    However, you can bind a temporary to const reference:

    {
       const TestClass& c = TestClass();
       //use c 
       //....
    }//<-------- the temporary will be destroyed here.
    

    In this case, the life of the temporary extends to the lifetime of the reference, i.e when the reference variable goes out of scope, the temporary will be destroyed as shown above.

    0 讨论(0)
  • 2020-12-17 06:42

    1) you can't get not const reference to a temporary object

    2) in the line c = TestClass(10); operator=(...) is called

    0 讨论(0)
  • 2020-12-17 06:50

    A good way is to compare references to pointers... (references are usually implemented the same way in assembly generally by using the ebx register). The main difference is that reference is constant after initialization...

    However, The line const TestClass& c = TestClass(); is parallel to const TestClass* const pc = &TestClass(); so the object will be create and destroyed on the stack, pc will still hold the same address.

    0 讨论(0)
提交回复
热议问题