std::move與std::vector問題記錄

簡介

該文章用于記錄學(xué)習中遇到的部分問題,可能不包含具體答案,會根據(jù)后續(xù)學(xué)習進行持續(xù)更新

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]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容