the "This" pointer.

I am really not getting what the "This->" pointer does. I have read and re-read my C++ book and I still cannot get what it does.

Can anyone help clarify in a simple example or explanation?


Thanks
First of all use "this->", not "This->".

"this" is a pointer to the object of which the function you're writing it into is a member of.
Let's have a simple example:

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

class MyClass {
public:
    void bar();
    unsigned char baz() { /*do something;*/ }

private:
    unsigned char foo;

}

void global_func(const MyClass&) { /*do something else;*/ }

void MyClass::bar()
{
/* from within this context the name foo would refer to this->foo */
    foo = 5;
    this->foo = 5; // same as before
    baz();
    this->baz(); // same as before too

/* anyways there are situations in which you can't do without the "this" pointer, let's say for 
example that you have to call an external function that uses the public interface of your class
 (from within this function). The only way to accomplish this is by using the this pointer, like: */
    global_func(*this);

}

int main()
{

    MyClass instance();

/* by doing the following call the "this" pointer inside the function bar equals (&instance) */
    instance.bar();

}


Also remember that using the "this" pointer from within a static member function is illegal since the call would be done without referring to any specific object.



Maybe someone can give you a better explanation though ;)
Last edited on
I want to give it a shot! :-D


Back in the old days, when there was no "Object Oriented", people used to write functions like this "increaseSalary":

1
2
3
4
5
6
7
8
struct Employee { int salary; };

void increaseSalary(Employee* e, int amount)
{ e->salary += amount; }

// give fred a raise.
Employee fred = ...;
increaseSalary(fred, 500);


Then some people thought, that actually "increaseSalary" should be directly a part of "Employee", since it always works with the employees. If you somehow attach the "increaseSalary" and all other functions that access the employees salary field directly with the Employee struct, then you can prohibit all *other* functions to access the salary (Nobody likes it, when anyone can freely see your salary, right?).

To explicitly tie the function "increaseSalary" to the struct "Employee", they introduced a new system of calling functions.

1
2
3
4
5
6
7
8
9
10
struct Employee
{
    int salary;
    void increaseSalary(int amount)
    { salary += amount; }
}

// give fred a raise.
Employee fred = ...;
fred.increaseSalary(500);


Noticed how the first parameter moved in front of the function call? You are probably already known to this kind of calling a function.

As the function "increaseSalary" is now directly coupled with the employee structure, you don't need to pass the employee as parameter anymore. All members of the employee are directly "merged" into your current scope.

However, sometimes you still *have* to access the old "first parameter" thing, for example if you want to forward call to another function that still takes a first-parameter (because, say, it's not so close coupled with Employees). Lets assume you have a HumanResource department that needs to get informed of the new salary. The department provides a function to inform of the raise, which you have to call.

1
2
3
// That's how to inform the HR about the new salary.
void informAboutRaise(Employee* employee);
...


that's how it would look like if there were no classes and structs (the old way)
1
2
3
4
5
void increaseSalary(Employee* e, int amount)
{
    e->salary += amount;
    informAboutRaise( e ); // pass the employee to the HR department
}


and that's how it looks now. Spot the usage of "this"?
1
2
3
4
5
6
7
8
9
struct Employee
{
    int salary;
    void increaseSalary(int amount)
    {
        salary += amount;
        informAboutRaise( this ); // <-- Here you need to pass the former "first parameter", which is this.
    }
}


"this" basically is the pointer to the struct itself which was used before the . when called to the member function.


Ciao, Imi.
PS: Writing "this->" is the way of explicitly saying, that you mean the variable from the enclosing struct.

Line 6 in my last code block example could also be written as
 
        this->salary += amount;


Both ways are the same, but some people prefere "this->salary" as it makes it crystal clear what "salary" you are talking about.
Last edited on
Topic archived. No new replies allowed.