Question: 1. What is the use of this pointer?
this
Pointer
• Definition : The this
pointer is an implicit pointer available inside all non‒static member
functions.
•
It stores the address of the object for which the member function is currently
being executed.
•
The keyword this identifies a special type of pointer. Suppose that you create
an object named x of class A and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f()
stores the address of x. You cannot
declare this pointer or make assignments to it.
•
A static member function does not have a this
pointer.
•
The this pointer is passed as a
hidden parameter to the member function call and it is available in the
function definition as a local variable. Below is an example in which this
pointer is used to refer the num
variable.
#include<iostream>
using namespace std;
class test
{
private:
int num;
public:
void get_val(int num)
{
//this
pointer retreives the value of obj.num
//this pointer is hidden by automatic variable num
this‒>num=num;
}
void print_val()
{
cout<<"\n
The value is "<<num;
}
};
int main()
{
test obj;
int num;
cout<<"\n
Enter Some Value";
cin>>num;
obj.get_val(num);
obj.print
_val();
return 0;
}
Enter Some Value 10
The value is 10
•
The this pointer points to the
object for which the member function is called. Hence from above code obj.get_val(num) can be interpreted as get_val(&obj,num); That means this pointer is passed as a hidden
argument to the called function by some automatic variable like num.
1. What is the use of
this pointer?
Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : C++ Program - this Pointer
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation