Keyword this

Hi all!
Could anybody explain the meaning and use of this? I searched various websites, but I didn't find a clear answer.
Thanks in advance!
it returns a reference to the current object:

1
2
3
4
5
6
7
8
9
10
11
class myObj {
    myObj* getPointer() {
        return(this);    //returns a pointer to the current object instance
    }
}

int main() {
    myObj m();
    myObj* p_m;
    p_m=m.getPointer;
}


At least thats what i think
The this pointer always points to the object that is being used to call the function. Consider the situation:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Example
{
  int data;
public:

  Example(int data);
  ~Example();
}
Example::Example(int data)
{
  // by using "this", we can avoid confusion
  this->data = data;
}
(a better way to avoid confusion is to not use clashing names like that)
(a better way to avoid confusion is to not use clashing names like that)


This. Pun intended :)

I can't think of a situation when it's a good idea to use clashing names and this.
So, in the example of LowestOne, this points to the parameter data, and not the data of the class?
Other way around.


'this' points to the object on which the member function was called:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class C
{
public:
  void func()
  {
    cout << this << endl;
  }
};

int main()
{
  C a, b;

  a.func();  // prints the address of a
  cout << &a << endl;  // has matching output with above
  // since func() was called on object 'a', this will point to 'a' inside the function


  b.func();  // prints the address of b
  cout << &b << endl;  // matches b.func()'s output
  // this time, since func() was called on object 'b', this points to 'b'
}
Last edited on
@OP: The other posters here have covered the basics of what the keyword this does, and as you can see is of very limited use to a beginner programmer. But now how about a more practical, albeit more complicated example?

Let's say you want to create an object that helps you manage your Windows without having to bounce all over the API. There is a structure in Windows called "STARTUPINFO" that holds some pretty useful information and you would like to include that information in your object (this is just an example, you can do this with almost any API or object). Now the obvious answer might be to just include an instance of that structure in your object, but that means you would have to refer to that struct within your own object in order to use any of those properties. The solution to this is simple, you just have your object inherit from the STARTUPINFO struct. But then how do you use the "GetStartupInfo()" function to fill in all of this data if you can't refer to the object? The answer is Polymorphism, because your object is now a child of the struct "STARTUPINFO" you can treat a pointer to your object as a pointer to it's parent. This means that passing a reference to your object with the keyword this in your objects constructor will fill in the attributes inherited from the STARTUPINFO struct. To illustrate:
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
#include <iostream>

#define _WIN32_WINNT 0x05010300 //Windows XP

#include <windows.h>

void pause()
{
    std::cin.sync();
    std::cin.ignore();
}

struct OBJECT: STARTUPINFO
{
    private:
        
    public:
       OBJECT(): A(0), B(0), Title("Some Thing To Do")
       {
            cb = sizeof(STARTUPINFO);
            GetStartupInfo(this);
        }
        
    std::string Title;    
    int A;
    int B;
    
};



int main(int argc, char *argv[])
{
    OBJECT Object;
    
    std::cout << Object.Title << std::endl << Object.lpTitle << std::endl;
    
    pause();
    return EXIT_SUCCESS;
}


Now because this example is a console program a lot of the attributes don't exist so they won't be filled in but you can see that the example that I used 'lpTitle' will match the text in the title bar of the window. Although this may be more of an example of how Polymorphism works, it would not be possible without the keyword "this".
Thanks all for the help! And thanks, computergeek, for the effort of writing such a elaborate example :) I got it now.
Topic archived. No new replies allowed.