i started studying c++ and i found the class below in a tutorial. My question has to do with the constructor. the class is:
class point{ private: double *x; double *y; public: point(double x=1,double y=1); //.... }; and the constructor is:
point::point(double x,double y) { this->x = new double; *(this->x)=x; this->y = new double; *(this->y)=y; } i wanted to ask WHY is the following code wrong? why do i have to use "this"?
point::point(double x,double y) { x = new double; *x=x; y = new double; *y=y; }
xthe compiler thinks your referring to the parameter variablexand not the class variablex. and since paramater variablexis not a pointer it throws a compilation errorxandy(members and parameters); 2) becausexis not defined asdoublebut pointer todouble. Maybe the purpose is to give example, but pay attention to these notes.