Reputation: 27
How do I make a pointer within a method of a class point to the object that the method is being called on?
I want to define a pointer in the body of a public method of a class and have it point to the instance of the object that the method is being called on.
Here's my code for some more context:
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;
}
Upvotes: 0
Views: 100