Pointers in C++: 

A pointer is a derived data type that refers to another data variable by storing the variables memory address rather than data.
Declaration of the pointer variable is in the following form : 
Eg int * ptr; 
Here ptr is a pointer variable and points to an integer data type. 
We can initialize pointer variable as follows 
int a, *ptr; // declaration ptr = &a //initialization 

Pointers to objects: 
Consider the following eg item X; // where item is class and X is object 
Similarly, we can define a pointer it_ptr of type item as follows 
Item *it_ptr ; 
Object pointers are useful in creating objects at runtime. 
We can also access public members of the class using pointers. 
Eg item X; item *ptr = &X;
the pointer 'ptr' is initialized with address of X. 
we can access the member functions and data using pointers as follows 
ptr->getdata(); 
ptr->show(); 

this pointer: 
C++ uses a unique keyword called this to represent an object that invokes a member function. this is a pointer that points to the object for which this function was called. This unique pointer is automatically passed to a member function when it is called.

Important notes on this pointer:

1. this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
2. this pointer is not counted for calculating the size of the object.
3. this pointer is not accessible for static member functions.
4. this pointer is not modifiable.


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: