List of all available Websheets
Viewing cpp/cs104/cpp11/unique_ptr2 by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Run the code below to see a problem that will occur with our <tt>MyUniquePtr</tt> class. <br><br> Then try to prevent this kind of code from ever compiling. <strong>Think of a way to make the compile enforce the rule that a unique pointer is truly unique and no copies should be allowed.</strong> |
Remarks Comments, history, license, etc. | Copied from problem cpp/cs104/cpp11/unique_ptr (author: redekopp@usc.edu) Copied from problem cpp/cs104/cpp11/sharedptr1 (author: redekopp@usc.edu) |
Engine | |
Template / Reference solution |
using namespace std; template <typename T> class MyUniquePtr { \[ REDACTED \show: T* p_; ]\ public: MyUniquePtr(T* p) : p_(p) { } ~MyUniquePtr() { delete p_; } T& operator*() { return *p_; } T const & operator*() const { return *p_; } T* operator->() { return p_; } MyUniquePtr& operator++() // pre-inc { ++p_; return *this; } }; struct Dummy { Dummy() { cout << "Constructor called" << endl; } ~Dummy() { cout << "Destructor called" << endl; } }; void f1(MyUniquePtr<Dummy> ptr){ // use ptr in the function return; } int main() { MyUniquePtr<Dummy> ptr1(new Dummy); // Notice a copy of ptr1 will need to be made when we call f1 f1(ptr1); 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 | |
Add compiler flag(s) json list of flags e.g. ["-Wno-unused-variable"] | ["-std=c++11"] |
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'.