求C++砖家们看下这个代码的输出
#include <iostream>
using namespace std;
class A
{
public:
A(int i=0)
{
m=i; cout<<"Constructor called."<<m<<endl;
}
void Set(int i)
{m=i;}
void Print() const
{cout<<m<<endl;}
~A()
{cout<<"Destructor called."<<m<<endl;}
private:
int m;
};
void main()
{
const int N=5;
A my;
my=N;
my.Print();
}
/*输出结果
Constructor called.0
Constructor called.5
Destructor called.5
5
Destructor called.5
*/
请大神解释一下输出结果
[解决办法]
Constructor called.0
A my;
Constructor called.5
因为把N赋给my时,以N为参数创建一个A的临时变量
Destructor called.5
释放临时变量,临时变量的析构函数
5
my.Print();
Destructor called.5
my的析构函数