מה זה THIS

alex11m

New member
מה זה THIS

לדוגמא בקוד הזה:
circle &circle::eek:perator++() //pre increment { radius++; return *this; }​
ד"א הנה הCLASS עצמה:
class circle { privete: int radius; public: circle (int r) {radius = r;} circle () {radius = 0 ;} circle &operator++(); circle operator++(int); void show() { cout << "radius = " << radius << endl; }​
 

DadleFish

New member
this

this היא מילה שמורה המצביע לכתובת ההתחלה של האובייקט, שהסוג שלו הוא ה-class הנ"ל. למשל,
class Circle { ... Circle *YourAddress() { return this; } ... }; int main() { unsigned int A; unsigned int *pA = &a; Circle c; Circle *pC = c.YourAddress(); return 0; }​
כמו שלקחתי את הכתובת של A והכנסתי אותה ל-pA, כך יש באפשרותי גם לעשות עם ה-Circle, באותה צורה בדיוק; אבל כדי להדגים לך מה זה this, כתבתי פונקציה חדשה בתוך Circle, שתפקידה להחזיר את הכתובת של האובייקט (c במקרה הזה).
 
למעלה