Answered step by step
Verified Expert Solution
Question
1 Approved Answer
smart pointer /** * @file Foo.h * * @brief Defines a generic `Foo` object. */ #ifndef FOO_H #define FOO_H #include #include /** * @brief `Foo`
smart pointer
/** * @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: " : "") Building the Smart Pointer Class Begin by creating the class definition for UniquePtr shown below in a header file named UniguePtr.h. This version of UniquePtr is designed to wrap a pointer to an object of type Foo; you should find the source code defining the Foo class (Poo.h and Poo.cpp) in the Resources section of this assignment. Code Mustration class UniquePtr public: UniquePtr (Foo*ptr) UniquePtr); private: Foo* _ptri NOTE: Place both the class definition and method implementations in the header file. You need not write all your method definitions directly in the class definition; you can place them below. The reason to keep everything in one header file is that you will be converting your class to a template later in this lab; all template code must reside in a header file. Constructor The constructor takes an owning pointer to a Foo object; the UniquePtr will assume ownership of that object, saving the pointer in its _ptr attribute. The expectation is that the UniquePtr will be constructed directly with the result of a new operation, as in the following: UniquePtr fooPtr[new Foo; Implement the constructor now. Destructor The destructor will destroy the object that is being managed by the smart pointer. Implement the destructor now. Test Add code to a driver program that will test your ability to construct and destruct your smart pointer. Hint: You can use braces to create small sections of scope for the purpose of testing how objects behave when they go out of scope within the context of a running program. See example below: Exarmple Code std: :cout
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