std::move与std::vector问题记录

2018-08-22  本文已影响21人  Kai_Z

简介

该文章用于记录学习中遇到的部分问题,可能不包含具体答案,会根据后续学习进行持续更新

class Bashful {
public:
    Bashful() {
        std::cout << "Bashful constructor" <<std::endl;
    }
    Bashful(const Bashful& bash) {
        std::cout << "Bashful copy constructor" << std::endl;
    }
    Bashful& operator=(const Bashful& bash) {
        std::cout << "Bashful assignment constructor" << std::endl;
        return *this;
    }
    Bashful(Bashful&& bash) {
        std::cout << "Bashful move constructor" << std::endl;
    }
    Bashful& operator=(Bashful&& v) {
        std::cout << "Bashful assignment operator" << std::endl;
    }
    ~Bashful() {
        std::cout << "Bashful destructor" << std::endl;
    }
};
int main(int argc, char** argv)
{
    std::vector<Bashful> s;
    s.push_back(Bashful());
    Bashful bashful;
    s.push_back(bashful);
    return 0;
}

输出值

Bashful constructor
Bashful move constructor
Bashful destructor
Bashful constructor
Bashful copy constructor // why
Bashful copy constructor // why
Bashful destructor // why
Bashful destructor // desturctor for bashful
Bashful destructor // destructor for s[1]
Bashful destructor // destructor for s[0]
上一篇下一篇

猜你喜欢

热点阅读