Are data structures the right place for shared_ptr?

I am implementing a binary tree in C ++. Traditionally, I had a pointer to the left and a pointer to the right, but manual memory management usually ends with tears. Which leads me to my question ...

Are data structures the right place to use shared_ptr?

+5
source share
7 answers

I think it depends on where you use them. I assume that what you are going to do is something like this:

template <class T>
class BinaryTreeNode 
{
    //public interface ignored for this example
    private:
        shared_ptr<BinaryTreeNode<T> > left;
        shared_ptr<BinaryTreeNode<T> > right;
        T data;
}

, , . , , .

, , shared_ptr, shared_ptr , , node . , , - , , - auto_ptr. , - :

template <class T>
class BinaryTreeNode 
{
    //public interface ignored for this example
    private:
        auto_ptr<BinaryTreeNode<T> > left;
        auto_ptr<BinaryTreeNode<T> > right;
        T data;
}

- , shared_ptr, - , , node . , , shared_ptr, node .

+8

, boost:: shared_ptr < > , , .

, std:: auto_ptr < >

+3

, .

, . , ptr , ptr. ptr. , , .

+2

, , , .

, , , , , . !

, * , shared_ptr, , . , shared_ptr .

(* , , .)

+2

shared_ptr . node, - . , . , , , - . , , .

+1

shared_ptr, , , shared_ptr .

0
source

Do you even need pointers? It seems you could use boost::optional<BinaryTreeNode<T> > left, right.

0
source

All Articles