this-> pointer

As I am a beginner, I confuse the difference between this-> usages.

question 1.
this-> is used in draw.h

My understanding at draw constructor this-> is unnecessary? this->x=x; can say x=x ? Am I wrong?

question2

At Cycle.cpp, cycle class is derived class and calling based class draw class.

Cycle::Cycle(int x, int y, int radius) : Draw(x, y) {
this->radius = radius;
}

my understanding on this-> is unnecessary. Can I remove this-> ?


Thank you in advance


----------------------------------------
//Cycle.h

I have derived class and base class
#ifndef Cycle_H
#define Cycle__H

#include "draw.h"

class Cycle : public draw
{
private:
int radius;

public:
Cycle(int x, int y, int radius);

bool insidePoint(int x, int y);
double getArea();

.......
};


#endif
-----------------------------------------

//Cycle.cpp

// Cycle.cpp
#include <math.h>
#include "Cycle.h"


Cycle::Cycle(int x, int y, int radius) : Draw(x, y) {
this->radius = radius;
}

bool Cycle::insidePoint(int x, int y) {
int dx = this->x - x;
int dy = this->y - y;

return (radius)
}


double Cycle::getArea() {
return pow(radius, 2) * M_PI;
}
-------------------------------

//
// draw.h

#ifndef draw__H
#define draw__H

#include <iostream>

class draw
{

protected:
int x;
int y;

public:
draw(int x, int y) {
this->x = x;
this->y = y;
}

......
virtual bool insidePoint(int x, int y) = 0;
virtual double getArea() = 0;
};

#endif

in draw(int x, int y) x argument hides class variable with same name. So all instances of x in method will refer to argument, not member variable. To access it we should specify it usind pointer member access: this->x
Alternatively you could initialize it in initialization list: draw(int x, int y): x(x), y(y) {} or rename arguments, so x would refer to class member again:
1
2
3
4
5
draw(int x_, int y_)
{
    x = x_;
    y = y_;
}
question 1.
this-> is used in draw.h

My understanding at draw constructor this-> is unnecessary? this->x=x; can say x=x ? Am I wrong?
No, that would assign parameter x to itself.
Thks peter87, can I remove this->? what is this-> means?
"->" is an operator that means "(*ptr).member"

Like:
1
2
3
4
5
6
7
8
9
10
struct MyClass{
   int myVar;
};
int main(){
   Myclass object;
   MyClass* object_ptr = &object;
     //The following mean the same thing
  (*object_ptr).myVar = 5;
  object_ptr->myVar = 5;
}
In my case, this-> is unnecessary?
can I remove this->
Not in this case. For example:
1
2
3
Cycle::Cycle(int x, int y, int radius) : Draw(x, y) {
 this->radius = radius;
}


"radius" refers to a new value to be assigned the object's radius member variable.
"this->radius" refers to the current object's member radius variable.
Although the two variables have the same name, they are different variables entirely. Using "this->" will disambiguate the two variables. Another approach is this (but I could be wrong):
1
2
3
4
5
6
Cycle::Cycle(int x, int y, int newradius) : Draw(x, y) {
 radius = newradius;
 /*
   Now radius refers to member variable and newradius refers to parameter
 */
}
Thks for all. I like to confirm that..

Question 1 this-> can be omitted and initialize as draw(int x, int y): x(x), y(y) {}
Question 2 this-> can not be removed

Thank you very much
Topic archived. No new replies allowed.