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

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.
PropertyValue
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)
remove
Engine
Template / Reference solution
 
#include <memory>
#include <iostream>
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
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'.