Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

this pointer

Each time your program creates a class instance (e.g. a and b in the following
program), C++ creates a special pointer called this, which contains the address of
the current object instance. Each time your program invokes an instance method
(e.g. a.init()), the compiler preassigns a special pointer named this to point to the
object instance. !he value of this pointer changes with di"erent instance
invocations. C++ recogni#es the this pointer only when a nonstatic member of the
object instance is e$ecuting. !he instances, in turn, use the this pointer to access
the di"erent methods. %&'
Every member function of a class has an implicitly de(ned constant pointer called
this. !he type of this is the type of the class of which the function is member. )t is
initiali#ed when a member function is called to the address of the class instance for
which the function was called. %*'
!he following statement is used to return the value to which this points to currently.
%&'
return (*this);
!he following program illustrates the concept of this pointer. %+]
1 class point {
2 public:
3 void print() {cout << ( << x << , << y << );
! void init(doubl" u, doubl" v) {x#u;y#v;
$ point inv"rs"(){x#%x; y#%y; return(*this);
& point '()"r"*a+*,() {r"turn t)is;
- privat":
. doubl" x, y;
/ ;
10 int +ain()
11 {
12 point a, b;
13
1! a.init(1.$, %2.$);
1$ a.print();
1& cout << 1n a is at << a.()"r"*a+*,() << "ndl;
1- b#a.inv"rs"();
1. b.print();
1/ cout << 1n b is at << b.()"r"*a+*,() << "ndl;
20
(*.,, +.,)
a is at
-$--./fdd/
(*.,, +.,)
b is at
0utput
1ictorial presentation of this
pointer
this pointer
2hether in class member functions the this pointer is e$plicitly used or not, the C+
+ compiler accesses all class members by means of an implicit this pointer %*'. !he
following code illustrates this fact.
!he this pointer is useful when it is needed during e$ecution of a class member
function to get a 3handle4 on the class object used to call the function. 5ecause, in
a member function, the class variable with which the function was called is out of
scope, the this pointer is provided as that 3handle4.
Note: Class instance, object instance, class variable means the same thing and has been used
interchangeably in this tutorial.
Reference:
*. Connor 6e$ton, 7ewnes C++ 1ocket 5ook, +
nd
edition
class t)is*ptr{
public:
void init(c)ar 'str) { strcpy(2na+", str);
void s)o(*(it)*t)is(void)
{cout << 3a+": << t)is%42na+" << "ndl;
void s)o(*(it)out*t)is(void)
{ cout << 3a+": << 2na+" << "ndl;
privat":
c)ar 2na+"530];
;

void +ain(void)
{
t)is*ptr a;
a.init(6lic");
a.s)o(*(it)*t)is();
a.s)o(*(it)out*t)is();

1 point inv"rs"(){
2 point c;
3 c.init(3,&);
! x#%x;
$ y#%y;
& 't)is#c;
- r"turn('t)is);
.
/ void +ain(void)
10 {
11 point a;
12 a.init (1,2);
13 a.inv"rs"();
1!
this pointer
+. )ra 1ohl, C++ for C 1rogrammers, &
rd
edition
&. 8ris 9amsa, 9amsa:s C;C++;C< 1rogrammer:s 5ible, +
nd
edition

You might also like