【发布时间】:2012-09-29 09:04:43
【问题描述】:
#include <iostream>
using namespace std;
class Ex {
private:
int i;
float f;
public:
Ex(int i,float f):i(i),f(f) {
cout << this->i << '\t' << this->f << endl;
}
~Ex(){
cout << "destructor";
}
};
int main() {
Ex i(10,20.1f);
}
在我上面写的程序中,如果构造函数是参数化的构造函数,如下所示:
Ex(int i,float f){
i=i;
f=f;
cout << this->i << '\t' << this->f << endl;
}
这里对象的数据成员被初始化为垃圾,因为数据成员由于同名的局部变量而被隐藏。 但是在上面的程序中,没有明确的 this.How 可以正常工作?
【问题讨论】:
标签: c++ oop constructor initialization