this pointer
Hello guys,
I'm very green in c++ and just started to understand classes and objects. I'm reading on the web about this pointer, but having difficulties understanding how it works exactly, could aanybody please help me understand or reference a good article for total noobs in c++ to grasp the this concept in simple explanation? would be greatly appreciated.
thank you
andre
atman
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
I rarely need to use it. If you are within the scope of the class, MyVariable is the same as this->MyVariable. It is sometimes just nice for other people that read your code so that they know where these variables reside.
Dave
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
I agree that I generally don't use the this pointer.
About the only time I use it is when I call a method or function outside of the class that needs to refer back to my object.
An example might be registering my object with a 'server object' for callbacks.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
>... the person who understand these pointer is the person who is well understanding the programming concepts.
Read a sentence backwards - that's right ;)..
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
this pointer has its own use:
first of all if you are dealing in a big project, you may use the convention this-member to differentiate between the member of the same global identifiers.
Moreover, it increases the readability of the code.
Secondly, you may like to use it if u do operator overloading for your class, like when you are overloading the assignment operator, you could return the this pointer itself.
But then, it is a fact that pointer do mess things up. moreover, C++ STL provide you with all those things you will hardly need to know about pointers.
This is what encapsulation is. And thats why OOP rocks.
But then, understanding pointers have its own merits. So I prefer that you should probably read about it surely.
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140