I have the following code:
template<class T>
class RandomTreeNode {
private:
RandomTreeNode<T> *left;
RandomTreeNode<T> *right;
public:
RandomTreeNode(): left(0), right(0) {}
void create_left_child(){ left = &RandomTreeNode<T>();}
void create_right_child(){ right = &RandomTreeNode<T>();}
But this gives me a compile error because I am pointing to the address of a temporary variable. I don’t want the new RandomTreeNode’s being created to be destroyed at the end of the function, how can I achieve this?
Use:
And remember you have to delete them when you no longer need them (probably when the entire tree is destructed, or when that particular node gets deleted).