Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟣 OOP  ·  Lesson 41

this Pointer

this pointer क्या है?

हर non-static member function के अंदर एक छिपा pointer होता है जिसे this कहते हैं, जो उस object पर इशारा करता है जिस पर function call हुआ। जब आप obj.method() लिखते हैं, method के अंदर this pointer obj का address रखता है।

नाम स्पष्ट करना

सबसे आम इस्तेमाल: जब किसी parameter का नाम data member जैसा ही हो, this-> स्पष्ट करता है कि आप किसका मतलब रखते हैं:

C++
#include <iostream>
using namespace std;
class Box {
    int width;
public:
    void setWidth(int width) {
        this->width = width;    // member = parameter
    }
    int getWidth() { return width; }
};
int main() {
    Box b;
    b.setWidth(50);
    cout << b.getWidth();   // 50
}
Output:
50

यहाँ this->width member है; सादा width parameter है।

Chaining के लिए *this लौटाना

अगर कोई member function *this (वर्तमान object reference से) लौटाए, आप calls को एक के बाद एक chain कर सकते हैं:

C++
class Builder {
    int x = 0, y = 0;
public:
    Builder& setX(int v) { x = v; return *this; }
    Builder& setY(int v) { y = v; return *this; }
    void show() { cout << x << "," << y << "\n"; }
};
int main() {
    Builder b;
    b.setX(3).setY(7).show();   // chained calls
}
Output:
3,7

हर setter वही object लौटाता है, तो अगली call उसी पर जारी रहती है।

Objects की तुलना

this किसी function को वर्तमान object की दूसरे से तुलना भी करने देता है, जैसे self-assignment जाँचने को: if (this == &other) return *this;।

this और static functions

Static member function के पास कोई this pointer नहीं होता, क्योंकि यह किसी object से बँधा नहीं। इसीलिए static function per-object data सीधे इस्तेमाल नहीं कर सकता।

आम गलतियाँ

  • this->member (arrow) के बजाय this.member (dot) इस्तेमाल करना — this एक pointer है।
  • Object को reference से लौटाने का मतलब होने पर *this के बजाय this लौटाना।
  • Static member function में this के होने की उम्मीद करना।
  • जहाँ नाम टकराव न हो वहाँ this-> का अति-इस्तेमाल (तब यह वैकल्पिक है)।
🏋️ अभ्यास

एक Rectangle class लिखें जिसमें setWidth और setHeight हर एक *this लौटाएँ, ताकि आप r.setWidth(4).setHeight(5) लिख सकें। एक area() जोड़ें और chained calls का परिणाम print करें।

सारांश

  • this उस object का छिपा pointer है जिस पर member function चलता है।
  • Member को same-नाम parameter से अलग बताने को this->name इस्तेमाल करें।
  • Method chaining सक्षम करने को *this लौटाएँ।
  • this एक pointer है, तो इसके साथ arrow operator इस्तेमाल करें।
  • Static member functions के पास this pointer नहीं होता।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में this pointer क्या है?
this pointer हर non-static member function के अंदर उपलब्ध एक छिपा pointer है जो उस object पर इशारा करता है जिस पर function call हुआ। यह member function को वर्तमान object को स्पष्ट रूप से refer करने देता है, जैसे this->name या *this लौटाकर।
C++ में this pointer क्यों इस्तेमाल होता है?
यह member और parameter को अलग बताने को इस्तेमाल होता है जब उनका नाम एक हो, वर्तमान object लौटाने को ताकि calls chain हो सकें, और वर्तमान object को किसी अन्य function को भेजने को। यह "यह function जिस object पर चल रहा है" उसे नाम से उपलब्ध कराता है।
C++ में return *this का क्या मतलब है?
*this लौटाना वर्तमान object का ही reference लौटाता है। यह method chaining सक्षम करता है, जहाँ कई calls एक पंक्ति में लिखी जाती हैं जैसे obj.setX(1).setY(2), क्योंकि हर function वही object अगली call को सौंपता है।
क्या C++ में static member function के पास this pointer होता है?
नहीं। Static member function किसी object के बजाय class के लिए होता है, तो इसके पास this pointer नहीं होता और यह इसके ज़रिए per-object members refer नहीं कर सकता। केवल non-static member functions को this pointer मिलता है।
C++ में this और *this में क्या अंतर है?
this वर्तमान object का pointer है, तो आप इसे arrow operator से इस्तेमाल करते हैं जैसे this->member। *this उस pointer को dereference करके object खुद देता है, जो आप तब लौटाते हैं जब वर्तमान object को reference से लौटाना हो।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicAccess Specifiers: public, private, protected आपकी प्रगति: 41 / 81 (51%) · Lesson 42 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें