Binary Search Tree Destructor

前端 未结 6 1370
太阳男子
太阳男子 2021-02-04 13:51

Working on implementing my own BST in C++ for the experience of dealing with such structures.

I\'ve had trouble implementing a destructor. I found in my studies, that on

6条回答
  •  借酒劲吻你
    2021-02-04 14:32

    Just use a destructors. It sounds like your problem is that you were trying to call the destructors directly, but the language handles this for you. They're called either when you leave a stack allocated object's scope, or when you delete an object.

    All you should need is this:

    ~BinSearchTree() {
        delete left;
        delete right;
    }
    

    delete will call their destructors.

    Note: It's perfectly safe to delete NULL, it has no effect.

提交回复
热议问题