Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The first two picture has the question context, the question is in the third picture. Thanks! Recall how weak pointers work. Weak pointers are an

The first two picture has the question context, the question is in the third picture. Thanks! image text in transcribed
image text in transcribed
image text in transcribed
Recall how weak pointers work. Weak pointers are an extension of shared (strong) pointers such that a weak pointer to an object won't be considered in the reference count, and objects can be deleted even if there are weak references to them. Consider this partial implementation of weak pointers. It has the following properties: - each smart pointer (shared or weak pointer) points to a control block which contains: strong_count: the number of shared_ptr references to the object weak_count: the number of weak_ptr references, plus one if strong_count >0 ptr: the pointer to the object - objects are destroyed when the strong_count reaches 0 - control blocks are destroyed when the weak_count reaches 0 - weak pointers cannot be directly used; the user must first promote it to extract a strong pointer that can then be dereferenced if the object has been destroyed, promote fails and returns nullptr we cannot dereference weak pointers directly because the object could have been destroyed (expired) Don't overthink implementations! They will be at most a few lines each. -shared_ptro f I/ your implementation here 3 Il other operators similar to p4, but update strong_count wh friend class weak_ptreT>; /I allow weak_ptr to have access to shared_ptriticb 3 templatectypename T> class weak_ptr\} control_block* cb public: I/ weak pointers are constructed from shared pointers, II since a weak pointer with no strong references is automatically expired weak ptr(const shared_ptr rhs) : cb(rhs.cb) \{ cb->weak_count, add fetch(1); 3 weak.ptr(const weak_ptr\& rhs) : cb(rhs.cb) \{ cb-sweak_count, add_fetch(1); 33 weak_ptr( ) II your implementation here 3 bool is expired ) const \{ /l your implementation here \} shared_ptr promote() \{ Il your implementation here 3 I/ copy/move constructors and operators are the same as p4, but update weak_count Finish the shared_ptr (a) constructor and (b) destructor implementation. Remember that strong_count is the number of shared_ptr references and weak_count is the number of weak_ptr references, plus one if strong_count is nonzero

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB.NET Databases

Authors: Thearon Willis

1st Edition

1594864217, 978-1594864216

More Books

Students also viewed these Databases questions