#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);
}
In the program above I wrote above,if the constructor was parameterized constructor like the following:
Ex(int i,float f){
i=i;
f=f;
cout << this->i << '\t' << this->f << endl;
}
here the data members of the object are initialized to junk because data members are hidden due to local variables of same name.
But in the program above it works fine without explicit this.How?
Similar questions have been asked before, e.g. here and here, but while many answers point out that this is (contrary to what Konstantin D – Infragistics says) not compiler-specific, I couldn’t find any answer that actually quotes the relevant parts of the Standard.
So here they are. I’ve added emphasis to highlight the key statements.