#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);
}
在程序上面我寫了上面,如果構造就像參數的構造函數如下:初始化列表,局部變量VS數據成員
Ex(int i,float f){
i=i;
f=f;
cout << this->i << '\t' << this->f << endl;
}
這裏對象的數據成員初始化爲垃圾因爲數據成員由於同名的局部變量而被隱藏。 但是,在上面的程序中,它沒有明確這一點工作正常。如何?
這不是編譯器實現的細節。它是C++標準所要求的。 – jogojapan
@jogojapan謝謝,我糾正了我的帖子。 –