Question

Transforming into shared pointers questions. Language: C++ I'm attempting to transform the following into a shared...

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?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Transforming into shared pointers questions. Language: C++ I'm attempting to transform the following into a shared...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT