#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 がなくても問題なく動作します。