2017-03-01 7 views
-1

更新の質問: 私はなぜ私は建設されている。 そして、どこでそれを傾けることができますか? 私は大学でC++の本を読んでいますが、私はそれを見つけました。どのようにコンストラクタの仕事

何か間違いがありますのでごめんなさい。

#include <vector> 
#include <string> 
#include <iostream> 

struct President { 
    std::string name; 
    std::string country; 
    int year; 

    President(std::string p_name, std::string p_country, int p_year) 
     : name(std::move(p_name)) 
     , country(std::move(p_country)) 
     , year(p_year) 
    { 
     std::cout << "I am being constructed.\n"; 
    } 
    President(President&& other) 
     : name(std::move(other.name)) 
     , country(std::move(other.country)) 
     , year(other.year) 
    { 
     std::cout << "I am being moved.\n"; 
    } 
    President& operator=(const President& other) = default; 
}; 

int main() 
{ 
    std::vector<President> reElections; 
    std::cout << "\npush_back:\n"; 
    reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936)); 

    for (President const& president : reElections) { 
     std::cout << president.name << " was re-elected president of " 
        << president.country << " in " << president.year << ".\n"; 
    } 
} 

出力:

一back: 私が構築されています。 私は動いています。

\ nありがとうございました。

+0

あなたを混乱させる原因は何ですか? 'President(" Franklin Delano Roosevelt "、" USA "、1936)' –

+0

クラスの*オブジェクト*、*インスタンス*を作成すると、そのオブジェクトは*構築され、適切なものが作成されます。コンストラクタが呼び出されています。 –

+0

あなたはcppreferenceからあなたの例を得ました。 cplusplus.com/referenceとcppreference.comは、2つの異なる、競合するオンラインC++リファレンスです。 – Cubbi

答えて

1

クラスのインスタンスを作成すると、自動的にコンストラクタが呼び出されます。では、あなたがとベクトルの要素据え付ける「ネルソン・マンデラを、そしてとき押し戻す」フランクリン・ルーズベルト」をしているとき、「社長」のプログラムのコンストラクタが呼び出されている。

+0

私のミスのために申し訳ありません。私は更新の質問 –

1

ここでの例は、おそらくことを示すように意図されます代わりにstd::vector::emplace_back構造が、push_back移動

我々は(より詳細な)社長

のバージョン
struct President { 
    std::string name; 
    std::string country; 
    int year; 

    President(std::string p_name, std::string p_country, int p_year) 
     : name(std::move(p_name)) 
     , country(std::move(p_country)) 
     , year(p_year) 
    { 
     std::cout << "I am being constructed at " << this << ".\n"; 
    } 
    President(President&& other) 
     : name(std::move(other.name)) 
     , country(std::move(other.country)) 
     , year(other.year) 
    { 
     std::cout << "I am being moved from " << &other << " to " << this << ".\n"; 
    } 
    President(const President& other) 
     : name(other.name) 
     , country(other.country) 
     , year(other.year) 
    { 
     std::cout << "I am being copied from " << &other << " to " << this << ".\n"; 
    } 
    ~President() 
    { 
     std::cout << "I am being destructed at " << this << ".\n"; 
    } 
}; 

を使用して何が起こるかの詳細を見ることができる出力例:

emplace_back: 
I am being constructed at 0x2b2b57e17c30. 

push_back: 
I am being constructed at 0x7ffcb9a0cec0. 
I am being moved from 0x7ffcb9a0cec0 to 0x2b2b57e17cb0. 
I am being destructed at 0x7ffcb9a0cec0. 

Contents: 
Nelson Mandela was elected president of South Africa in 1994. 
Franklin Delano Roosevelt was re-elected president of the USA in 1936. 
I am being destructed at 0x2b2b57e17cb0. 
I am being destructed at 0x2b2b57e17c30. 
+0

私の間違いのために申し訳ありません。更新質問でした –

関連する問題