很难理解对象生命周期、复制、移动构造函数

问题描述

我正在学习 C++ 并尝试了解移动构造函数和复制构造函数的对象生命周期。我在 vscode 中使用 g++ 9.2.0、c++17。 当我为纸牌游戏编写以下代码时,输​​出让我非常困惑......

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>

static const std::vector<std::string> Suits {"Club","Diamond","Heart","Spade" };
struct Card{
    std::string suit = "Blank";
    int value = 0;

    Card()                                          {puts("default constructed");} 
    Card(std::string s,int v): suit(s),value(v)   {std:: cout << this->suit << '\t';puts("card constructed");}
    Card(const Card& cc)                            {std:: cout << this->suit << '\t';puts("copy constructed:");} //copy constructor
    Card(Card && mc)                                {std:: cout << this->suit << '\t';puts("move constructed:");} //move constructor
    Card& operator= (const Card& cc)                {std:: cout << this->suit << '\t';puts("copy assign");  suit = cc.suit; value = cc.value; return *this;} //copy assignment
    Card& operator= (Card&& mc)                     {std:: cout << this->suit << '\t';puts("move assign");  suit = std::move(mc.suit);  value = std::move(mc.value); return *this;}  //move assignment
    ~Card()                                         {std:: cout << this->suit << '\t';puts("card destroyed");}
};


struct Deck{
    std::vector<Card> cards;
    int num_remain{0} ;

    Deck(): cards(std::vector<Card>{}),num_remain(0){puts("Deck constructed");}    
    ~Deck()                                         {puts("Deck destroyed");}
    
    void build_deck(); 
};

void Deck::build_deck(){

    puts("start builing----------------------");
    cards.clear();
    num_remain = 0;
   

        for (auto s : Suits){
            for (int v = 1; v <= 3; ++v){ 
                cards.emplace_back(s,v);  
                num_remain++;
                std::cout << num_remain << '\n';
                puts("==========Card Added===============");
            }
        }
        

    puts("finish building-----------------------");
    return;
}


int main(){
    
    Deck draw; 
    Deck discard; 
    puts("~~~two deck created~~~"); 
    draw.build_deck();
    return 0;
}

终端输出

Deck constructed
Deck constructed
~~~two deck created~~~
start builing----------------------
Club    card constructed
1
==========Card Added===============
Club    card constructed
Blank   copy constructed:
Club    card destroyed
2
==========Card Added===============
Club    card constructed
Blank   copy constructed:
Blank   copy constructed:
Blank   card destroyed
Club    card destroyed
3
==========Card Added===============
Diamond card constructed
4
==========Card Added===============
Diamond card constructed
Blank   copy constructed:
Blank   copy constructed:
Blank   copy constructed:
Blank   copy constructed:
Blank   card destroyed
Blank   card destroyed
Club    card destroyed
Diamond card destroyed
5
==========Card Added===============
Diamond card constructed
6
==========Card Added===============
Heart   card constructed
7
==========Card Added===============
Heart   card constructed
8
==========Card Added===============
Heart   card constructed
Blank   copy constructed:
Blank   copy constructed:
Blank   copy constructed:
Blank   copy constructed:
Blank   copy constructed:
Blank   copy constructed:
Blank   copy constructed:
Blank   copy constructed:
Blank   card destroyed
Blank   card destroyed
Blank   card destroyed
Blank   card destroyed
Diamond card destroyed
Diamond card destroyed
Heart   card destroyed
Heart   card destroyed
9
==========Card Added===============
Spade   card constructed
10
==========Card Added===============
Spade   card constructed
11
==========Card Added===============
Spade   card constructed
12
==========Card Added===============
finish building-----------------------      
Deck destroyed
Deck destroyed
Blank   card destroyed
Blank   card destroyed
Blank   card destroyed
Blank   card destroyed
Blank   card destroyed
Blank   card destroyed
Blank   card destroyed
Blank   card destroyed
Heart   card destroyed
Spade   card destroyed
Spade   card destroyed
Spade   card destroyed

谁能解释为什么不同循环之间存在不一致?

我之前使用过 push_back,它似乎比使用 emplace_back 像这里的代码那样构造更多的空白卡副本。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)