I have abstract class Managee and helper class Wrapper. Pointer to Managee used to construct Wrapper, then Wrapper will take ownership over Managee. I want to ensure that user will always allocate new Managee. Are rvalue-references suitable for this goal?
Wrapper definition:
...
Wrapper(Managee * && tmpptr);
Managee & GetManagee();
...
Wrapper usage:
Wrapper a(new ManageeA()); // ok;
Wrapper b(&a.GetManagee()); // error? <-----
Rvalue-references don’t help, since
&a.GetManagee()is an rvalue.Why not take a
std::unique_ptr?Usage:
For
make_unique, see here.The best solution, however, wouldn’t even allow the user to create a
Managee‘s derived types on the stack – this can be done with a factory function (of course withstd::unique_ptr) and making constructors private: