kyword this

guyz!!!!can u please guide me about that keyword "this" which is used with "->" that operator.what is meant by "this" with "->" in c++.................thnxs !!!!
Last edited on
The 'this' keyword references the name of the calling object. So if I wrote some code such as:
1
2
3
4
5
6
7
8
9
10
11
someObject.shootGun();

void class name ::  shootGun()
{
// 'this' is a pointer automatically created when an object calls a function of its.
this->getBulletCount(); // the arrow operator (->) dereferences 'this' pointer
// you could write that line of code another way
(*this).getBulletCount(); //because the * operator also dereferences a pointer
// or you could just call the function without using 'this' because its implied
getBulletCount();
}

when i write code i generally use the 'this' pointer because it helps organize the function calls with their objects. the dot operator gets the objects information (functions or variables) but cant be used with 'this' because a reference (pointer) only points to an object but is not actually the object. so you need to dereference (go get the object in other words) it so it becomes the object.

Summary:
-> : is used as a way to dereference a pointer to get the object
* : is used as a way to dereference a pointer to get the object
. : is a way to access information on an object
Another useful operator:
& : gets the address of anything stored in memory

Information can be accessed two ways:
from the data type itself (.)
or from the reference pointer (->, *)

does that help?
Last edited on
Topic archived. No new replies allowed.