mercredi 24 mars 2021

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?

Aucun commentaire:

Enregistrer un commentaire