Answered step by step
Verified Expert Solution
Question
1 Approved Answer
need c++ format class UniquePtr { public : UniquePtr(Foo* ptr); ~UniquePtr(); private : Foo* _ptr; }; ************************************************************** /** * @file Foo.h * * @brief Defines
need c++ format class UniquePtr{ public: UniquePtr(Foo* ptr); ~UniquePtr(); private: Foo* _ptr; };
**************************************************************
/** * @file Foo.h * * @brief Defines a generic `Foo` object. */ #ifndef FOO_H #define FOO_H #include#include /** * @brief `Foo` will report to stdout whenever objects are * constructed or destroyed. * @detailed `Foo` is useful for testing templated containers or * any code where tracking individual object instances * over time is important. * The objects are default-constructible, copy-safe, * and can provide their unique serial number, and * print identifying information to an output stream. */ class Foo{ public: Foo(const std::string& = ""); ~Foo(); int serial() const; std::string tag() const; private: static int _count; int _serial; std::string _tag; }; std::ostream& operator **********************************************************************************************
/** * @file Foo.cpp * * @brief Implementations for the generic Foo class. * * @detailed Foo is simply designed to report when constructed and * destroyed, and each Foo object has a unique serial * number, so they can be tracked. */ #include "Foo.h" /** * c-tor for `Foo` that allows an optional string "tag" to be * attached in addition to the serial number of the object. * The construction of the object will be announced to stdout. */ Foo::Foo(const std::string& tag) : _serial{++_count}, _tag{tag} { // If tag is empty, set it to the serial number std::cout 0 ? " Tag: " : "") 0 ? " Tag: " : "") Assignment In lab you developed a templated version of a "smart pointer class similar to the std: :uniquept. In this assignment, you will use what you learned to create a reference-counting smart pointer that can be used to share ownership of a resource. This pointer will be similar to the std: :shared ptr Contents [hide] Reference Counting The concept of reference counting is simple: The smart pointer keeps a counter attribute that indicates the number of copies of that particular pointer object that currently exist. When a new copy is made, the reference count is increased by one; when a copy is destroyed the count decreases by one. When the last copy is destroyed, the reference count would drop to zero-indicating that the managed resource should be deleted. In the diagram below, a resource is being managed by a single copy of a reference-counting pointer. In this situation, the pointer's reference count is 1, and if the pointer is deleted or goes out of scope, it should delete the managed resource as well (since it has the only reference to the resource). 1.1 Reference Counting 1.2 class Shared Ptr 1.3 Testing 1.4 Limitations pointer ref count Dynamic Resource The diagram below shows the same resource being managed by four separate copies of a reference-counting pointer. Notice that each copy of the pointer must contain the same reference count (4 in this case).If any one of the pointers were deleted or went out of scope, the reference count in al of the remaining pointers must drop to 3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started