[zdd]的一个疑问
#include <iostream>
using namespace std;
struct A
{
A() { cout << "A()" << endl; }
//A( const A& ) { cout << "A(A)" << endl ;}
A& operator=( const A& ) { cout << "A=A" << endl; }
~A() { cout << "~A()" << endl; }
};
A foo( A a )
{
cout << "---" << endl;
return a;
}
int main(void)
{
{
foo( A() );
}
system( "pause" );
return 0 ;
}
在 VC++2005 下输出为:
A()
---
~A()
~A()
~A()
为什么 return a; 会挪用2次析构函数?
———————————- 简化一下———————————-
#include <iostream>
using namespace std;
struct A
{
A() { cout << "A()" << endl; }
//A( const A& ) { cout << "A(A)" << endl ;}
A& operator=( const A& ) { cout << "A=A" << endl; }
~A() { cout << "~A()" << endl; }
};
void foo( A a )
{
}
int main(void)
{
{
foo( A() );
}
system( "pause" );
return 0 ;
}
VS2005中的独特现象描写:
假如没有 A( const A& ) { cout << "A(A)" << endl ;}
那么输出
A()
~A()
~A()
假如加上 A( const A& ) { cout << "A(A)" << endl ;}
那么输出
A()
~A()