I'm doing a school homework with c++ (i'm still learning). i'm trying to implement randomly generated binary tree structures, using shared_ptr to store information of a Node on multiple places(i need that for the homework). Consider the following sample code(this is my little test program):
#include <vector>
#include <memory>
struct Node : public std::enable_shared_from_this<Node> {
char charValue;
int intValue;
std::shared_ptr<Node > left;
std::shared_ptr<Node > right;
std::shared_ptr<Node > parent;
std::shared_ptr<Node> getPtr()
{
return shared_from_this();
}
Node() : intValue(0)
{
charValue = 0;
}
};
int main(int argc, char**argv) {
std::vector<std::shared_ptr<Node>> treeQueue;
std::shared_ptr<Node> root = std::make_shared<Node>();
treeQueue.clear();
treeQueue.push_back(root->left); //std::shared_ptr<Node>(root->left)); //root->left->getPtr());
treeQueue.push_back(root->right); //std::shared_ptr<Node>(root->right)); //root->right->getPtr());
treeQueue[1] = std::make_shared<Node>(); //std::shared_ptr<Node>(new Node);
system("PAUSE");
return 0;
}
In this case, i intialize a root,and i want to keep every other node of a tree empty until i choose it in the tree structure. And in my homework, i decide which Node i'm gonna choose after i push it in the vector treeQueue. (I randomly pick it from there).
THE PROBLEM: For example, in the code above, when i initialize treeQueue[1], i expect root->right to be also initialized. Because they're the same pointers. But it stays empty! I also put the alternative ways I've tried to push it in treeQueue (which also didn't work). I've tried "enable_shared_from_this" too,which is why it's there.
Is there a way to do this? Or is there another technique that will provide the same functionality that i need?
I learned that it's not healthy to use raw pointers so i used shared_ptr which makes sense, but now i get this problem. Please help me, i'm gonna lose my mind. I searched everywhere i could. I tried everything i found.
shared_ptr
shares what the ptr
is pointing to. The shared_ptr
itself is not "shared" in any way, shape, matter, or form. Each shared_ptr
that references the same object (that's being shared), is its own, separate, discrete shared pointer.
Here, you copied one shared_ptr
to another shared_ptr
. Then you replaced the copy of a shared_ptr
with another, newly-constructed, shared_ptr
.
That newly-constructed shared_ptr
has nothing to do with the original shared_ptr
that the first one, the one that was copied from, is referencing.
Your code is equivalent to the following:
int *a=NULL;
int *b=a;
b=new int{4};
Do you expect, now, *a
to return 4
?. Of course not. They are two completely different pointers.
Thanks to Sam Varshavchik for making me understand my mistake. And i ended up also taking Jarod42's suggestion and used treeQueue as
std::vector<std::shared_ptr<Node>*>
and it worked. So i'm marking this comment as the solution, as a reference to those two comments :)
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.