computer 版 (精华区)
发信人: remember (Learning C++), 信区: program
标 题: 什么时候会产生临时对象(第二个问题)
发信站: 听涛站 (2001年10月25日21:31:48 星期四), 站内信件
你说,显然是2嘛,a声明的时候调用无参构造函数,
A=n这句编译时生成一个临时对象,即A=n这句扩展如下:
A temp(n);
a=temp;
temp.A::~A();
所以输出count的结果为2....嗯,但下面一段程序输出结果又是多少呢?
注意,这次以数组方式声明对象.....
class A {
public:
A() { count++; }
A(int i) { count++; }
~A() {}
static int getcount() { return count; }
private:
static int count;
};
int A::count=0;
int main() {
A a[]= { A(),A(1) };
cout<<"count is:"<<A::getcount()<<endl;
return 1;
}
--
洛阳亲友如相问,一片冰心在玉壶
: ※ 来源:·听涛站 tingtao.dhs.org·[FROM: 匿名天使的家]
--
洛阳亲友如相问 一片冰心在玉壶
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:0.929毫秒