'this' pointer


I dont really understand the 'this' pointer. You need it to initialize its data members, but yet its optional for using it in the member functions, and apparently calling the member functions as well. Is there any way to remove the need to use the this pointer in the constructor? IT seems like it should be there all or nothing sort of. is "this" pointer essentially the equivalent of Python's "self"?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

using namespace std;

class Character{
    int health, strength, defense, defense_init, strength_init;
    public:
        Character(int health, int strength, int defense){
            this->health = health;
            this->strength = strength;
            this->strength_init = strength;
            this->defense_init = defense;
            this->defense = defense;
            update_defense();
            update_strength();
        }
        
        void update_defense(){
            defense = defense_init * health;
        }
        
        void update_strength(){
            strength = strength_init * health;
        }
        
        void take_hit(int hit){
            health -= hit;
            if (health <= 0){
                health = 0;
            }
            update_defense();
            update_strength();
        }
        
        void test(){
            cout << health << endl;
            cout << strength << endl;
            cout << defense << endl << endl;
        }
};

int main(){
    Character player(100, 3, 2);
    player.test();
    player.take_hit(2);
    player.test();
    player.take_hit(25);
    player.test();
    player.take_hit(100);
    player.test();

}
metulburr wrote:
You need it to initialize its data members, but yet its optional for using it in the member functions

Technically not. The reason you need it in your constructor there is because you have parameter names that match member names.

1
2
3
4
5
6
7
8
9
10
class Foo
{
private:
   int bar;
public:
   Foo( int bar )
   {
      this->bar = bar;  // 'this' referring to member, not parameter
   }
};


You could avoid it by having different parameter names. Or, preferably, by using initialisation lists for you constructor. They can distinguish between variables with the same name without the explicit this pointer.

1
2
3
4
5
6
7
class Foo
{
private:
   int bar;
public:
   Foo( int bar ): bar( bar ) {}
};
you can initialize your variables this way also.

look up member intializer list

you have to use : then variablename ( parameter) followed by a comma
and then the {}

replace this
1
2
3
4
5
6
7
8
9
 Character(int health, int strength, int defense){
            this->health = health;
            this->strength = strength;
            this->strength_init = strength;
            this->defense_init = defense;
            this->defense = defense;
            update_defense();
            update_strength();
        }


with

1
2
3
4
5
6
7
8
9
10
11
 Character(int health, int strength, int defense)
			: health(health), strength(strength) , strength_init(strength), defense(defense), defense_init(defense)
		{
            //this->health = health;
            //this->strength = strength;
            //this->strength_init = strength;
            //this->defense_init = defense;
            //this->defense = defense;
            update_defense();
            update_strength();
        }

Oh ok.

It seems easier to do this. Never heard of initialisation lists before. But will look into it. Thanks. IT looks like really weird syntax.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

using namespace std;

class Character{
    int health, strength, defense, defense_init, strength_init;
    public:
        Character(int health_, int strength_, int defense_){
            health = health_;
            strength = strength_;
            strength_init = strength_;
            defense_init = defense_;
            defense = defense_;
            update();
        }
        
        void update(){
            update_defense();
            update_strength();
        }
        
        void update_defense(){
            defense = defense_init * health;
        }
        
        void update_strength(){
            strength = strength_init * health;
        }
        
        void take_hit(int hit){
            health -= hit;
            if (health <= 0){
                health = 0;
            }
            update();
        }
        
        void test(){
            cout << health << endl;
            cout << strength << endl;
            cout << defense << endl << endl;
        }
};

int main(){
    Character player(100, 3, 2);
    player.test();
    player.take_hit(2);
    player.test();
    player.take_hit(25);
    player.test();
    player.take_hit(100);
    player.test();

}
Last edited on
Topic archived. No new replies allowed.