如何在类的方法中使指向该方法被调用的对象的指针?
我想在类的公共方法的主体中定义一个指针,并使它指向正在调用该方法的对象的实例。
这是我的代码,用于更多上下文:
void Node::print() {
Node *temp = this; //points to the node that calls the print function
while (temp->next != NULL) {
cout << "Name: " << temp->name << "\tID: " << temp->ID << endl;
temp = temp->next; //makes temp->next point to the next node in the list.
}
//this line runs when temp->next == NULL
cout << "Name: " << temp->name << "\tID: " << temp->ID << endl;
}