美文网首页
std::move与std::vector问题记录

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

作者: Kai_Z | 来源:发表于2018-08-22 15:33 被阅读21次

    简介

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

    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]
    

    相关文章

      网友评论

          本文标题:std::move与std::vector问题记录

          本文链接:https://www.haomeiwen.com/subject/mmlyiftx.html