0

I am interested in understanding exactly what the member access operator is referring to at the recursive function calls,this->addNode. Thanks for any help in understanding what the function is being called on.

void BinarySearchTree::addNode(Node* node, Bid bid) {

    // If node > bid, add to left subtree
    if (node->bid.bidId.compare(bid.bidId) > 0) {
        if (node->left == nullptr) {
            node->left = new Node(bid);
        }
        // If left node exists, keep traversing down left subtree
        else {
            this->addNode(node->left, bid);
        }
    }
    // If node < bid, add to right subtree
    else {
        if (node->right == nullptr) {
            node->right = new Node(bid);
        }
        // If right node exists, keep traversing down right subtree
        else {
            this->addNode(node->right, bid);
        }
    }
}
xkcdjerry
  • 965
  • 4
  • 15
Ryan M
  • 1
  • 1
  • 3
    Does this answer your question? [Can you explain the concept of the this pointer?](https://stackoverflow.com/questions/4483653/can-you-explain-the-concept-of-the-this-pointer) – Quimby Jul 31 '21 at 15:40
  • In the case shown the `this` is redundant and does not add anything. A better use case would be in template code. – drescherjm Jul 31 '21 at 15:59
  • `this` refers to the instance variable (of the class considered) on which the calls apply (recursive calls have nothing special in this respect). Inside a method definition, `this->` is superfluous, as it is implied, unless another variable has the same name as the invoked property/method. –  Jul 31 '21 at 16:02
  • It doesn't look like this needs to be a non-static member function at all. – Nathan Pierson Jul 31 '21 at 16:03
  • Ok, so this-> addNode(...) could have just been addNode(...)? The this-> is unnecessary? – Ryan M Jul 31 '21 at 16:50
  • @RyanM It is often redundant. It can be useful for readability to be explicit. – eerorika Jul 31 '21 at 16:50
  • Thanks, drescherjm, Yves, and Nathan. This kind of thing is confusing for a new programmer. It works, but the professor didn't explain the use and it seems he was just on a roll using this->. I'll try not using the access operator here and maybe I'll understand it better. I'm brand new and can't mark a response as helpful. – Ryan M Jul 31 '21 at 17:13
  • It's just calling `addNode`. The `this->` is noise. – Pete Becker Jul 31 '21 at 20:11

0 Answers0