问题及代码
ALL rights reserved.
*文件名称: 初学对象6
作者:李长鸿
*完成时间:2015.4.15
*问题描述: 阅读程序
*/
#include <iostream>
using namespace std;
class example
{
public:
example()
{
cout<<"Default Constructing! "<<endl;
}
example(int n)
{
i=n;
cout<<"Constructing: "<<i<<endl;
}
~example()
{
cout <<"Destructing: "<<i<<endl;
}
int get_i()
{
return i;
}
private:
int i;
};
int sqr_it(example o)
{
return o.get_i()* o.get_i();
}
int main()
{
example x(10);
cout<<x.get_i()<<endl;
cout<<sqr_it(x)<<endl;
return 0;
}
总结:10 和 100之间 多了一行Constructing 10。不是形参是类时,实参赋值给形参时也要去类里走一遍吗??又理解错误???!!
本文通过一个C++示例程序探讨了构造函数与析构函数的调用时机,特别是当函数参数为类实例时,构造函数如何被调用以及其背后的原理。
472

被折叠的 条评论
为什么被折叠?



