0

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; } 
3
  • 21
    Please throw that tutorial into garbage. Commented Sep 11, 2016 at 11:43
  • 2
    Because when you refer to x the compiler thinks your referring to the parameter variable x and not the class variable x. and since paramater variable x is not a pointer it throws a compilation error Commented Sep 11, 2016 at 11:44
  • 1
    The example is very stupid. 1) because of duplicate of variable name x and y (members and parameters); 2) because x is not defined as double but pointer to double. Maybe the purpose is to give example, but pay attention to these notes. Commented Sep 11, 2016 at 12:00

3 Answers 3

4

How would your compiler know the difference between x the parameter and x the field of the class ?

this->x means the x variable that belongs to the class I'm in i.e: the point class

If you called the field x in the class _x, you could write *_x = x in the constructor

PS: Yes, agreed with the comments, this is NOT a good tutorial x)

For more information : http://en.cppreference.com/w/cpp/language/this

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think the sentence "this->x means the x variable that belongs to the scope I'm in i.e: the point class" uses the correct terminology. this isn't about scope, it's about using the current object members instead of using the current scope (the function, which would find the function parameter x).
Yeah sorry, I was looking for a way to phrase it correctly.
Bad habbit x) Not sure where it comes from
2

Just want to add bit additional information to Naliwe answer,
You can do the constructor like this:

 point::point(double x,double y) : x(new double(x)), y(new double(y)) { } 

In this case, the compiler understands which name refer which "thing" - e.g. first x is for class field, second is for constructor argument.

2 Comments

Thank you, I hesitated, because it involves other concepts, like the fact that using the constructor list, your fields aren't initialized 'twice' i.e default constructed then initialized to whatever you want :) Although in this particular case, he uses pointers
this is the preferable C++ way. however in this particular code there is an error that might lead to memory leak and / or program crash. in C++11 you should use smart pointers, but you will learn for them later.
0

Perhaps you might try using scope qualifiers:

point::point( double x, double y ) { point::x = new double; *point::x = x; point::y = new double; *point::y = y; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.