test_method()
호출 후 test(2)
개체가 파괴 된 이유를 누가 알 수 있습니까?임시 개체 Modyfying
#include<iostream>
#include<string>
using namespace std;
class test
{
int n;
public:
test(int n) : n(n)
{
cout << "test: " << n << endl;
}
~test()
{
cout << "~test: " << n << endl;
}
test & test_method()
{
cout << "test_method: " << n << endl;
return *this;
}
};
int main(int argc, const char *argv[])
{
cout << "main start" << endl;
const test &test1 = test(1);
const test &test2 = test(2).test_method();
cout << "main end" << endl;
}
출력된다 :
main start
test: 1
test: 2
test_method: 2
~test: 2
main end
~test: 1
'std :: cout'이 버퍼되어 있으므로 디버그가 더 이상 필요하지 않습니다. – triclosan