共用方式為


大括弧初始化

對於 定義 的 class 建構函式並非總是必要,尤其是相對簡單的建構函式。 使用者可以使用統一 class 初始化來初始化 或 struct 的物件,如下列範例所示:

// no_constructor.cpp
// Compile with: cl /EHsc no_constructor.cpp
#include <time.h>

// No constructor
struct TempData
{
    int StationId;
    time_t timeSet;
    double current;
    double maxTemp;
    double minTemp;
};

// Has a constructor
struct TempData2
{
    TempData2(double minimum, double maximum, double cur, int id, time_t t) :
       stationId{id}, timeSet{t}, current{cur}, maxTemp{maximum}, minTemp{minimum} {}
    int stationId;
    time_t timeSet;
    double current;
    double maxTemp;
    double minTemp;
};

int main()
{
    time_t time_to_set;

    // Member initialization (in order of declaration):
    TempData td{ 45978, time(&time_to_set), 28.9, 37.0, 16.7 };

    // When there's no constructor, an empty brace initializer does
    // value initialization = {0,0,0,0,0}
    TempData td_emptyInit{};

    // Uninitialized = if used, emits warning C4700 uninitialized local variable
    TempData td_noInit;

    // Member declaration (in order of ctor parameters)
    TempData2 td2{ 16.7, 37.0, 28.9, 45978, time(&time_to_set) };

    return 0;
}

class當 或 struct 沒有建構函式時,您可以依 成員在 中 class 宣告的順序提供清單專案。 class如果 具有建構函式,請依參數的順序提供專案。 如果類型具有隱含或明確宣告的預設建構函式,您可以使用大括弧初始化搭配空白大括弧來叫用它。 例如,下列 class 專案可以使用空白和非空白大括弧初始化來初始化:

#include <string>
using namespace std;

class class_a {
public:
    class_a() {}
    class_a(string str) : m_string{ str } {}
    class_a(string str, double dbl) : m_string{ str }, m_double{ dbl } {}
double m_double;
string m_string;
};

int main()
{
    class_a c1{};
    class_a c1_1;

    class_a c2{ "ww" };
    class_a c2_1("xx");

    // order of parameters is the same as the constructor
    class_a c3{ "yy", 4.4 };
    class_a c3_1("zz", 5.5);
}

如果類別具有非預設建構函式,則類別成員出現在大括弧初始化運算式中的順序是建構函式中對應的參數出現順序,而不是宣告成員的順序(如 class_a 上一個範例所示)。 否則,如果類型沒有宣告的建構函式,成員初始化運算式必須以與宣告的順序一樣出現在大括弧初始化運算式中。 在此情況下,您可以視需要初始化任意數目的公用成員,但無法略過任何成員。 下列範例顯示沒有宣告建構函式時,在大括弧初始化中使用的順序:

class class_d {
public:
    float m_float;
    string m_string;
    wchar_t m_char;
};

int main()
{
    class_d d1{};
    class_d d1{ 4.5 };
    class_d d2{ 4.5, "string" };
    class_d d3{ 4.5, "string", 'c' };

    class_d d4{ "string", 'c' }; // compiler error
    class_d d5{ "string", 'c', 2.0 }; // compiler error
}

如果明確宣告預設建構函式,但標示為已刪除,則無法使用空白大括弧初始化:

class class_f {
public:
    class_f() = delete;
    class_f(string x): m_string { x } {}
    string m_string;
};
int main()
{
    class_f cf{ "hello" };
    class_f cf1{}; // compiler error C2280: attempting to reference a deleted function
}

您可以在通常進行初始化的任何位置使用大括弧初始化,例如,做為函式參數或傳回值,或使用 new 關鍵字:

class_d* cf = new class_d{4.5};
kr->add_d({ 4.5 });
return { 4.5 };

/std:c++17 模式和更新版本中,空白大括弧初始化的規則會稍微限制一點。 請參閱 衍生建構函式和擴充匯總初始化

initializer_list建構函式

initializer_list 類別 代表可在建構函式和其他內容中使用的指定型別物件清單。 您可以使用大括弧初始化來建構initializer_list:

initializer_list<int> int_list{5, 6, 7};

重要

若要使用此類別,您必須包含 < initializer_list > 標頭。

initializer_list可以複製 。 在此情況下,新清單的成員是原始清單成員的參考:

initializer_list<int> ilist1{ 5, 6, 7 };
initializer_list<int> ilist2( ilist1 );
if (ilist1.begin() == ilist2.begin())
    cout << "yes" << endl; // expect "yes"

標準程式庫容器類別,以及 stringwstringregex 都有 initializer_list 建構函式。 下列範例示範如何使用這些建構函式執行大括弧初始化:

vector<int> v1{ 9, 10, 11 };
map<int, string> m1{ {1, "a"}, {2, "b"} };
string s{ 'a', 'b', 'c' };
regex rgx{ 'x', 'y', 'z' };

另請參閱

類別和結構
建構函式