Skip to main content

Posts

Showing posts with the label this

What is this Pointer in C++?

 Pointers are always headache for Programmers. However C++ have a special pointer which is referred as ' this '. The this pointer is one of the important concept in C++ and can be really confusing too. So in this article I will be discussing about the this pointer and its role in C++. The this Pointer Let us see an example first. # include < iostream > using   namespace  std; class   A { int   a ; public: // constructor A ( int   x   =   0 ): a ( x ){} void   display ()  const {      cout << a << ' \n ' ; } }; int   main (){ A   objA ( 10 ); A   objB ( 11 ); objA . display (); objB . display (); return   0 ; } It is very clear that the output will be 10 11 The program looks very simple, But do you know whats going on, under the Hood? How does the function remember, which object is invoking it? This is where the this pointer comes in action. Actually, the Compiler is secretly passing a constant pointer pointing to the object