首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求C++砖家们看上这个代码的输出

2012-10-21 
求C++砖家们看下这个代码的输出#include iostreamusing namespace stdclass A{public:A(int i0){mi

求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的析构函数

热点排行