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

简介

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

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]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容