How C++ reference works

前端 未结 5 1145
别那么骄傲
别那么骄傲 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: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.

提交回复
热议问题