2021年3月24日星期三

Why string data is not moved to target but vector was

I have below code:

class string  {      std::string data;    public:        string()      {          data = "Hello";      }      string(const string& that)      {          data = std::move(that.data);          std::cout << data << " and that.data " << that.data;      }  };    int main()  {      std::vector<int> vec = { 1, 2, 3, 4, 5 };      std::cout << "vec size = " << vec.size() << std::endl;      std::vector<int> nvec = std::move(vec);      std::cout << "nvec size = " << nvec.size() << std::endl;      std::cout << "vec size = " << vec.size() << std::endl;      string s;      string s1(s);  }  

The output of code is:

vec size = 5                                                                                                              nvec size = 5                                                                                                             vec size = 0                                                                                                              Hello and that.data Hello     

Why did not the that.data moved to target data as like nvec and vec?

https://stackoverflow.com/questions/66792890/why-string-data-is-not-moved-to-target-but-vector-was March 25, 2021 at 12:00PM

没有评论:

发表评论