简体   繁体   中英

Using a shared_ptr with objects

I have a struct named Board . In this struct, I have an attribute called parentBoard which is supposed to just point to another Board object. I was originally going to use regular pointers to do this, but I found something in the stl for shared_pointers . If I define the shared_ptr like this: shared_ptr<Board> pointerToParentBoard; , how can I make this point to an already defined Board object?

I have already tried this, hoping it was just like a regular pointer:

pointerToNewBoard = &board1;

But alas, it isn't and I can't find anything on Google to help. Any help is greatly appreciated!

EDIT: In my project, memory leaks are very costly and it might be difficult to keep track of all the allocated memory, which is why I'm wanting to use smart pointers.

Keep parent pointer raw but manage children by smart pointer.

struct Board
{
   private:
   std::shared_pointer<std::vector<Board>> children;
   Board * parent;
   public:
   Board(Board * ptr=nullptr):children(std::make_shared<std::vector<Board>>())
   {
        // don't add here, it becomes recursive
        parent = ptr;
   }

   // or add here
   Board addNew()
   {
       children->push_back(Board(this));
       return children->back();
   }

   // or add here
   void add(Board * ptr)
   {
       children->push_back(*ptr);
   }

   // make parent
   // should only use if "this" is a root node
   void makeParent(Board * b)
   {
       b.add(this);
       if(nullptr==parent)
           parent = b;
       else
           throw std::runtime_error("This node is not root. Can't change parent.");
   }

}

this way, children can be independently deallocated automatically when only their parent is deallocated, meaning that a child's scope is its parent.

Also don't forget to add constructors for rule of three/five.

Avoid using raw pointers in c++ to avoid accidental memory leaks. To avoid cyclic reference, use std::weak_ptr for the parent member.

struct Board
{
private:
    std::weak_ptr<Board> _parent;

public:
    Board(std::shared_ptr<Board> parent = nullptr) {
        _parent = parent;
    }

    std::shared_ptr<Board> getParent() {
        std::shared_ptr<Board> strong = std::shared_ptr<Board>(_parent);
        return strong ? strong : nullptr;
    }

    std::shared_ptr<Board> traverseToRoot() {
        std::shared_ptr<Board> grand_parent = getParent();
        std::shared_ptr<Board> parent = grand_parent;
        while (!grand_parent) {
            parent = grand_parent;
            grand_parent = grand_parent->getParent();
        }
        return parent;
    }
};

int main()
{
    std::shared_ptr<Board> greatgrandparent = make_shared <Board>();
    std::shared_ptr<Board> grandparent = make_shared <Board>(greatgrandparent);
    std::shared_ptr<Board> parent = make_shared <Board>(grandparent);
    auto great_great_grand_parent = parent->traverseToRoot();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM