C Programming and Data Structures (16 Blogs)

How To Implement This Pointer in C++?

Published on Aug 08,2019 2.9K Views

How To Implement This Pointer in C++?

While programming you might’ve come across ‘this’ keyword. ‘this’ is a pointer that points towards the caller objects. In this post, we will dive deeper into the concept of this pointer in C++

Following pointers will be covered in this article,

Let us start with this article on This Pointer in C++

This Pointer In C++

Pointers to Objects

People tend to stay away from pointers as their working sound little confusing. In this post, we will understand the concept of the pointer in the simplest way possible. Pointers are used to hold the address of a particular variable. They are used to refer the variable whose address it is storing. One important point to note here is that a pointer can only store the address of a variable whose type matches the type of the pointer. In other words, an int type pointer can only hold the address of an int type variable.

What should be the type of the pointer which is used to store the address of an object? To find an answer to this question we need to understand what is the type of a particular object? Int, char, float? No, an object is of type class. In other words, the type of an object is the class to which it belongs. As a particular class is a user-defined data type and an object of that class belongs to that type.

Till now, you might’ve made pointers to reference variables of primitive data types. Let’s see how can we use pointers which can refer to a particular object.

Moving on with sample code for Pointers to Objects

Syntax

class_name *pointer_name;

#include <iostream>
using namespace std;
class Car{
public:
int Number_of_wheels;
int Number_of_passengers;
void getinfo(int x, int y){
Number_of_wheels = x;
Number_of_passengers = y;
}       
void showinfo(){
cout<<"Number of Wheels = "<<Number_of_wheels<<"n";
cout<<"Number of Passengers = "<<Number_of_passengers<<"n"; } }; int main() { Car car; Car *ptr; ptr=&car; ptr->getinfo(4,5);
ptr->showinfo();
Car car2;
car2.getinfo(6,8);
car2.showinfo();
return 0;
}

Output

Number of Wheels = 4

Number of Passengers = 5

Number of Wheels = 6

Number of Passengers = 8

This is how we can use a pointer to refer an object.

Note the. operator is used with the name of the object and -> operator is used while accessing the method through a pointer.

Moving on with this article on This Pointer in C++

This Pointer

If you are with python you might’ve come across the word ‘self’. The functionalities of ‘this’ and ‘self’ are similar to each other. ‘This’ is a parameter which is passed to all non-static methods of a class which we cannot see but can be used in any non-static or instance methods of a class. ‘This’ pointer is passed to a non-static member function as soon as it is called. It is an implicit argument to all non-static member functions of a class.

Moving on with sample code for This Pointer

#include <iostream>
using namespace std;
class Car{
private:
int Number_of_wheels;
int Number_of_passengers;
public:
void getinfo(int x, int y){
this->Number_of_wheels = x;
this->Number_of_passengers = y;
}        
void showinfo(){
cout<<"Number of Wheels = "<<Number_of_wheels<<"n";
cout<<"Number of Passengers = "<<Number_of_passengers<<"n";
cout<<"Address of the current object is = "<<this<<"n"; } }; int main() { Car car; Car *ptr; ptr=&car; ptr->getinfo(4,5);
ptr->showinfo();
Car car2;
car2.getinfo(6,8);
car2.showinfo();
return 0;
}

Output

Number of Wheels = 4                                                                                                

Number of Passengers = 5                                                                                            

Address of the current object is = 0x7ffdbac81740                                                                   

Number of Wheels = 6                                                                                                

Number of Passengers = 8                                                                                             

Address of the current object is = 0x7ffdbac81748

The above program gives us a brief understanding of ‘this’ keyword. ‘this’ keyword is useful when the variable name is clashing or when multiple objects in a method are involved. 

Pointers to derived class

Pointers can not only be used to refer to the base class but they can also be used to point at a derived class object. For example, if class Car inherits from class Vehicles, a pointer of type Vehicles can also be used to point towards an object of type Car.

Vehicles *ptr;
Vehicles vehicles;
Car car;
ptr = &vehicles;
ptr = & car;

The only catch here is that if we are using a base class pointer to point towards the derived class object we will be able to access only base class methods which are inherited by the derived class object. We can not access the members of the derived class using the base class pointer.

 If a member of the class Car has the same name as one of the members of class Vehicles then, in that case, the pointer will access the base class member.

Thus we have come to an end of this article on ‘This Pointer in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement This Pointer in C++?

edureka.co