0

I wrote a class which contains move constructor and move assignment operator. I want to know where and how these move semantics are used in application?

class Base
{
public:
 Base():iptr(new int(10)){}
 ~Base(){}

 Base(const Base& b):iptr(new int(*b.iptr))
 {
     cout << "Copy ctor" << endl;
 }
 Base& operator=(const Base& b)
 {
     iptr.reset(new int(*b.iptr));
 }

 //move ctor
 Base(Base&& b)
 {
     cout << "Move ctor" << endl;
     iptr = std::move(b.iptr);
 }
 //move assign op
 Base& operator=(Base&& b)
 {
     cout << "Move assign op" << endl;
     iptr = std::move(b.iptr);
     return *this;
 }
 void printVal(){cout << *iptr << endl;}

private:
 std::unique_ptr<int> iptr;
};
deepdive
  • 9,720
  • 3
  • 30
  • 38
  • @MikeSeymour Nope. I want to know how applications will use this class and in particular where move semantics will be applied. – deepdive Apr 18 '14 at 12:37
  • 3
    Do the detailed answers to that question not explain how move semantics are used? In a nutshell: they're used to initialise objects from _rvalues_ of the same type - either objects that are about to be destroyed, or objects you explicitly apply `std::move` to - without doing a full copy. A full answer would be as long as those in the duplicate question, so I'm not going to try to write one. – Mike Seymour Apr 18 '14 at 12:40
  • There's also a good writeup [here](http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf) (albeit in the form of slides from a presentation). – Mike Seymour Apr 18 '14 at 12:42
  • I also think that the answers to the linked question show common use cases for the move semantics. – Hristo Iliev Apr 18 '14 at 12:43

0 Answers0