1
//C++ Made Easy HD 26 - Introduction to Classes
//Michael LeCompte

#include <iostream>
#include <string>

using namespace std;


class Player {
public:
    Player() {
        cout << "1st overload" << endl;
    }

    Player(int Health){
        cout << "2nd overload" << endl;
        this->health = Health;
    }

    Player(int Health, int attPow){
        cout << "3nd overload" << endl;
        this->health = Health;
        attackPower = attPow;
    }

    ~Player() {
        cout << "Player instance destroyed" << endl;
    }

    //Mutators
    void setHealth(int val) {
        health = val;
    }

    void setAttPow(int val) {
        attackPower = val;
    }

    void setDef(int val) {
        defense = val;
    }

    void setXp(int val) {
        xp = val;
    }

    //Accessors
    int healthVal() {
        return health;
    }

    int attPowVal() {
        return attackPower;
    }

    int defVal() {
        return defense;
    }

    int xpVal() {
        return xp;
    }

private:
    int health;
    int attackPower;
    int defense;
    int xp;
};


int main() {
    Player player[4] = {Player(2, 5), Player(65, 2), Player(2), Player(1)};
    cout << player[0].healthVal() << endl;
    cout << player[1].healthVal() << endl;
    cout << player[2].healthVal() << endl;

    system("pause");
    return 0;
}

From the code above the lines I am focusing on are the this->health = Health lines. I'm wondering why I need to use this->health instead of health = Health. I know it has something to do with the fact that I'm creating new Player objects with an array (I'm doing a tutorial on it). I just don't understand why it makes it so I have to use this-> or how it works. Thanks.

harper
  • 13,345
  • 8
  • 56
  • 105
Michaelslec
  • 905
  • 1
  • 9
  • 20
  • 5
    Who says you have to use `this->`? – jogojapan Jul 01 '13 at 09:25
  • 1
    You don't need to. You can use `health` in your member functions and it will be the same as `this->health` (unless the function takes a parameter named `health`). – gx_ Jul 01 '13 at 09:26
  • 1
    I think the `this->` in this case was added merely for clarity. IMHO, it should be `this->health = health`, since a capitalized variable name is somewhat unuaual style, and *then* you would indeed *have* to use `this->` to explicitly access the member instead of the parameter. – DevSolar Jul 01 '13 at 09:26
  • 3
    Regarding what the code _should_ look like: It _should_ not be using explicit assignment at all. What you have there is the implementation of a constructor, and in the constructor you should be using an initialization list: `Player(int health, int appPow) : health(health), attackPower(attPow) { /*...*/ }`. – jogojapan Jul 01 '13 at 09:31
  • if you are looking for the uses of "this" keyword then you can see [this keyword][1] [1]: http://stackoverflow.com/questions/6779645/use-of-this-keyword-in-c –  Jul 01 '13 at 09:53
  • I suggest you email the author of "C++ Made Easy HD 26 - Introduction to Classes" and tell him how his code could be refactored – doctorlove Jul 01 '13 at 10:13

6 Answers6

6

you don't to use this. health = Health; will work. but the proper way is to use initialization:

Player(int Health) : health(Health) {
  // constrcutor code
}
WoJo
  • 360
  • 2
  • 10
3
    Player(int Health){
    cout << "2nd overload" << endl;
    this->health = Health;
}

if your Health name was health with lower caption than you need to use the this pointer because otherwise c++ don't know you want to use the class variable and will use the parameter variable.

in your example c++ knows wich one to use (the difference is in the caption) and you can skip the this pointer.

But health should be with a lower caption throuh Naming Conventions

Bjorn
  • 457
  • 1
  • 7
  • 22
  • 1
    err, do you post that convention as recommended ad a thing generally agreed upon as a good idea? Or it is just a random company's pick of ideas as illustration? – Balog Pal Jul 01 '13 at 09:34
  • 1
    It was just a example that normally a variable starts with a lowercase but i will delete the link – Bjorn Jul 01 '13 at 09:35
  • I'd expect a naming convention to address ideas on name hiding -- for this scenario I would avoid param name 'health' if actually used this way for some reason. the canonical way `Player(int health) : health(health) {}` would just work fine. – Balog Pal Jul 01 '13 at 09:49
2

You don't need to use this-> in your example. It's optional. Without this->, your code will be totally equivalent.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
1

This is C++, not python. Name lookup is such that this->member is always considered without prefix, only gets hidden if you introduce a local or function param to hide it. (Another special case is in templates to access base class members.)

Unfortunately some libraries like VTK made the this-> noise nonsense in their style so it spreads like cancer.

In your example there's no hiding, so no reason to use the prefix. As separate note, in the ctors good code would use init list rather than assignment, and make the 1-param version explicit. For those cases you can even use the same param name as the member.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
1

The only time I use "this" is when I want to be explicit about what object I'm referring to. Namely, I use "this" in copy constructors and assignment operators.

It'sPete
  • 5,083
  • 8
  • 39
  • 72
0

If you had defined any local variable with the name health, then, in order to differentiate between this local variable with the data member of your class, you would need to strictly use this pointer. In other cases, using this pointer is also a good practice but not necessary.

fatihk
  • 7,789
  • 1
  • 26
  • 48