ไม่สามารถเข้าใจการใช้พารามิเตอร์นี้ในการสร้างคลาสใน c ++

class Node {
public:
    int key;
    Node *parent;
    std::vector<Node *> children;

    Node() {
      this->parent = NULL;
    }

    void setParent(Node *theParent) {
      parent = theParent;
      parent->children.push_back(this); // I can't understand this.
    }
};

ในฟังก์ชัน setParent ใน parent-›children.push_back(this) เหตุใดเราจึงส่งค่านี้เป็นพารามิเตอร์ และมันจะทำอย่างไร


person Gopal Nandan    schedule 15.07.2020    source แหล่งที่มา
comment
this โดยพื้นฐานแล้วเป็นตัวชี้ไปยังออบเจ็กต์ที่เรียก   -  person kesarling    schedule 15.07.2020
comment
สิ่งนี้ตอบคำถามของคุณหรือไม่? ตัวชี้ 'สิ่งนี้' คืออะไร   -  person xavc    schedule 15.07.2020
comment
สิ่งนี้ตอบคำถามของคุณหรือไม่? ตัวชี้ 'สิ่งนี้' คืออะไร   -  person kesarling    schedule 15.07.2020


คำตอบ (1)


ตัวชี้ this เป็นตัวชี้ไปยังวัตถุที่ฟังก์ชันสมาชิกกำลังดำเนินการอยู่ บรรทัดของโค้ดนั้นจะเพิ่ม this โหนดให้กับเวกเตอร์ลูกของพาเรนต์ นั่นสมเหตุสมผลแล้วเพราะ this โหนดเป็นหนึ่งในลูกของพาเรนต์

person David Schwartz    schedule 15.07.2020