← Back to C++ Course | Chapter 8: OOP Core | Lesson 8 of 14

C++ का this Pointer

this pointer का मतलब है 'वह object जिसके अंदर मैं अभी हूँ', me या my कहने जैसा। यह एक object को खुद को refer करने में मदद करता है।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
#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;
}

'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'

cpp
// 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;
}

'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

cpp
// 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;
}

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'

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. this->x की बजाय this.x लिखना, क्योंकि this एक pointer है।
  2. एक static member function के अंदर this इस्तेमाल करना, जहाँ यह exist नहीं करता।
  3. chaining के लिए intended एक method से *this की बजाय this return करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.