Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/cs104/cpp11/unique_ptr by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Complete the <tt>MyUniquePtr</tt> class below using RAII principles to ensure the object being pointed to by the internal data member gets deallocated automatically when the MyUniquePtr is deallocated.
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/cs104/cpp11/sharedptr1 (author: redekopp@usc.edu)
remove
Engine
Template / Reference solution
 
#include <memory>
#include <iostream>
using namespace std;
template <typename T>
class MyUniquePtr {
   T* p_;
 public:
   MyUniquePtr(T* p) : p_(p) { }
   ~MyUniquePtr() 
   { \[ REDACTED ]\ }
   T& operator*()  { return *p_; }
   T const & operator*() const { return *p_; }
   T* operator->() { return p_; }
   MyUniquePtr& operator++() // pre-inc
   { \[ REDACTED ]\ }
};
struct Dummy
{
   Dummy()  { cout << "Constructor called" << endl; }
   ~Dummy() { cout << "Destructor called" << endl; }  
   
};
int main()
{
   // A unique pointer should provide automatic deallocation.
   MyUniquePtr<Dummy> ptr1(new Dummy);
   
   // Use ptr1 and dummy object
   
   cout << "Goodbye" << endl;
   return 0;   
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[{}]
Is example?
i.e., just a demo
remove
Add compiler flag(s)
json list of flags
e.g. ["-Wno-unused-variable"]
 
["-std=c++11"]
remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.