First please look at the code
and the outputCode:#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; }
If I comment out "const Test& t = Test();" and use "Test();" instead, the output will beTest
haha
~Test
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?Test
~Test
haha