Ask coding questions

← Back to all posts
which pointer to use c++

i am making an interpreter for a language and i am wondering when i am making the ast whether i should be passing around a Node, Node*, std::shared_ptr<Node>, or std::unique_ptr<Node>. if it helps i would rather not have to deal with dynamically allocated objects i.e new and delete

Answered by ANDREWVOSS [earned 5 cycles]
View Answer

If you don't want to deal with dynamically allocated memory, then use unique_ptr or shared_ptr, which deal with it for you. The main difference is that there can only be 1 unique_ptr pointing to any given Node, while you can have any number of shared_ptrs pointing to a Node. I'd recommend unique_ptr, unless you need to have multiple pointers to a node.