【发布时间】:2011-12-11 07:16:39
【问题描述】:
我有一个简单的类测试:
class test{
public:
test(){};
test(int &input){a=input;};
~test (){};
int a;
};
鉴于构造函数参数是通过引用调用的,我曾期望我能够从 calss 外部操作引用成员,但事实并非如此:
int _tmain(int argc, _TCHAR* argv[]){
int c=10;
test t1(c);
c=20;
int d=t1.a;;}
d 这里给出的原始值是 10 而不是 20。为什么会这样以及如何更改构造函数以便能够通过 c 操作 a。
谢谢
【问题讨论】:
-
这里没有使用默认构造函数。
标签: c++ constructor reference