vendredi 22 décembre 2017

Why I can't move this vector in the copy constructor of my custom class?

class TestClass 
{
  public:
  TestClass(){
      cout<<"constructor"<<endl;
      p = {1,2,3};
      cout<<(unsigned int *)(this->p.data())<<endl;
  }
  TestClass(const TestClass& test):  p(std::move(test.p))
  {
      cout <<"copy constructor"<<endl;
      cout<<(unsigned int *)(this->p.data())<<endl;

  }
  TestClass(TestClass && test): p(std::move(test.p))
  {
      cout <<"move constructor"<<endl;
      cout<<(unsigned int *)(this->p.data())<<endl;
  }
  private:
      std::vector<int> p;
};


int main()
{
  TestClass t{};
  TestClass p{t};
  TestClass s{std::move(p)};
  return 0;
}

And the output is

 constructor
 0xb92bf0
 copy constructor
 0xb915b0
 move constructor
 0xb915b0

I am just wondering why the address below constructor is different from the one below copy constructor. From what I understand, even it is a copy constructor, but I used std::move to get a rvalue reference and vector's move constructor should be called, so they should be same object.

Aucun commentaire:

Enregistrer un commentaire