Transforming into shared pointers questions.
Language: C++
I'm attempting to transform the following into a shared pointer but I'm fairly confident I'm not doing it properly. To be honest I'm getting my dots and arrows all over the place right now and I'm struggling with shared pointers. I'm going to share only the code I'm having issues with as opposed to the entire code which is pretty massive.
(original Code).
void bfs(Node& start) {
myqueue.push(&start);
start.setColor(GRAY, clock);
start.setRank(0);
}
(Transformed code)
void bfs(shared_ptr<Node> start) {
myqueue.push(start);
start->setColor(GRAY, clock);
start->setRank(0);
}
My overall question is does this look correct? If not how would I change the shared pointer code to properly match what is happening in the original?
Hello, please find the below explanation for this question.
======================================================================================
Explanation :
You are writing a fabulous code. But, as i can see in the original code, you are trying to make reference object of class Node type and using it in the later part of original code. But, in case of Transformed code, you are making an object of Node class type which is using shared pointer to auto deletion of memory. This is the main issue between the reference object and object.
The transformed code should look like the below code:
======================================================================================
void bfs(shared_ptr<Node> &start) {
myqueue.push(&start);
start->setColor(GRAY, clock);
start->setRank(0);
}
======================================================================================
Please notice the highlighted part in code and try to run the code again with these changes. I hope you understood the explanation. In case of any query, please feel free to ask; i will be happy to clear them. And please don't forget to give a like to this solution.
Transforming into shared pointers questions. Language: C++ I'm attempting to transform the following into a shared...