C++ का this Pointer
In this page:
class ClassName {
data_type member;
public:
void method_name(data_type member) {
this->member = member; // this points to the current object
}
};
'this' Pointer समझना
C++ में हर non-static member function को implicitly उस object का एक hidden pointer मिलता है जिस पर इसे call किया गया, function के अंदर this की तरह accessible।
यही है कि एक method कैसे जानता है कि कौन से specific object का data operate करना है जब same class के कई objects exist करते हों।
उदाहरण: Understanding the 'this' Pointer
// Include std::cout and std::cin
#include <iostream>
// Define the type Box
class Box {
// Declare size and set it to 5
int size = 5;
// Members below can be accessed from outside the class
public:
void show() { std::cout << this->size << std::endl; }
};
// Program execution starts in main()
int main() {
Box b;
b.show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Variable Name Conflicts Resolve करना
जब एक constructor या method parameter एक member variable के साथ एक नाम share करता है, parameter नाम normally उस scope के अंदर member को shadow करेगा।
'this->memberName' explicitly लिखना compiler को बताता है कि आपका मतलब class के member variable से है, local parameter से नहीं।
उदाहरण: Resolving Variable Name Conflicts
#include <iostream>
class Point {
int x;
public:
Point(int x) {
this->x = x; // "this->x" is the member, "x" is the parameter
}
void show() { std::cout << x << std::endl; }
};
int main() {
Point p(7);
p.show();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
'this' के साथ Method Chaining
आप एक member function से '*this' को एक reference की तरह return कर सकते हैं, जो caller को एक single expression में same object पर कई method calls साथ chain करने देता है — obj.setX(1).setY(2).setZ(3) जैसे fluent interfaces के पीछे का pattern।
उदाहरण: Method Chaining with 'this'
// Include std::cout and std::cin
#include <iostream>
// Define the type Builder
class Builder {
// Declare value and set it to 0
int value = 0;
// Members below can be accessed from outside the class
public:
Builder &add(int n) { value += n; return *this; }
int get() { return value; }
};
// Program execution starts in main()
int main() {
Builder b;
// Print to the console with cout
std::cout << b.add(1).add(2).add(3).get() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
'this' को एक Argument की तरह Pass करना
आप this pointer को class के बाहर एक free function को एक argument की तरह pass कर सकते हैं, उस external function को calling object के data पर सीधे operate करने देते हुए जैसे यह खुद एक member function हो, बिना पूरे member-function access की ज़रूरत के।
उदाहरण: Passing 'this' as an Argument
// Include std::cout and std::cin
#include <iostream>
class Box;
void describe(Box *b);
// Define the type Box
class Box {
// Members below can be accessed from outside the class
public:
int size = 5;
void show() { describe(this); }
};
void describe(Box *b) {
// Print to the console with cout
std::cout << "Box size: " << b->size << std::endl;
}
// Program execution starts in main()
int main() {
Box box;
box.show();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constant Functions और 'this'
एक const member function के अंदर, this का type effectively एक constant object का pointer बन जाता है, जिसका मतलब है compiler उस function के अंदर किसी member variable को modify करने की कोई भी कोशिश reject करेगा — यही है कि const methods अपना read-only promise कैसे enforce करते हैं।
उदाहरण: Constant Functions and 'this'
#include <iostream>
class Box {
int size = 5;
public:
int getSize() const { return size; } // "this" is a pointer to a const object here
};
int main() {
const Box b;
std::cout << b.getSize() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
this->xकी बजायthis.xलिखना, क्योंकिthisएक pointer है।- एक static member function के अंदर
thisइस्तेमाल करना, जहाँ यह exist नहीं करता। - chaining के लिए intended एक method से
*thisकी बजायthisreturn करना।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: