在 C++ 中组合初始化 class
Compose initialize class in c++
我不确定这是否叫compose initialization
。基本上我想知道的是:
main
中的对象 foo
是如何初始化的
- 这种初始化有什么规则吗,因为我发现有时会失败。
给定以下代码:
#include <iostream>
#include <string>
#include <vector>
class Base {
public:
std::string str_mem;
int int_val = 0;
};
class Bar {
public:
Bar() = default;
Bar(int val) : int_val(val) {
}
auto GetVal() {
return int_val;
}
private:
int int_val = 0;
};
class Foo : public Base {
public:
int mem_1;
int mem_2;
int mem_3;
std::string str_mem;
Bar class_mem;
auto print() {
printf("[%d,%d,%d,%s,%d]\n", mem_1, mem_2, mem_3, str_mem.c_str(), class_mem.GetVal());
}
};
auto main(int argc, char** argv) -> int {
Foo foo{{"base1", 2}, 1, 2, 3}; // How `foo` is constructed, any constructor get called?
foo.print();
Foo foo1{{"base2", 2}, 1, 2, 3, "string", 3};
foo1.print();
}
这是aggregate initialization, Foo
is an aggregate type。
Each direct public base, (since C++17)
array element, or non-static class member, in order of array subscript/appearance in the class definition, is copy-initialized from the corresponding clause of the initializer list.
给定 Foo foo1{{"base2", 2}, 1, 2, 3, "string", 3};
,Base
的子对象从 {"base2", 2}
初始化(它也得到 aggregate-initialized),数据成员 mem_1
从1
、mem_2
从2
初始化,mem_3
从3
初始化,str_mem
从"string"
初始化(通过std::string
的构造函数采用 const char*
),class_mem
从 3
初始化(由构造函数 Bar::Bar(int)
)。
给定Foo foo{{"base1", 2}, 1, 2, 3};
,除了str_mem
和class_mem
是从空列表初始化的,前面的成员的初始化方式与上面相同,结果是default-initialized(由它们的默认构造函数)。
If the number of initializer clauses is less than the number of members and bases (since C++17)
or initializer list is completely empty, the remaining members and bases (since C++17)
are initialized by their default member initializers, if provided in the class definition, and otherwise (since C++14)
copy-initialized from empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates).
我不确定这是否叫compose initialization
。基本上我想知道的是:
main
中的对象foo
是如何初始化的- 这种初始化有什么规则吗,因为我发现有时会失败。
给定以下代码:
#include <iostream>
#include <string>
#include <vector>
class Base {
public:
std::string str_mem;
int int_val = 0;
};
class Bar {
public:
Bar() = default;
Bar(int val) : int_val(val) {
}
auto GetVal() {
return int_val;
}
private:
int int_val = 0;
};
class Foo : public Base {
public:
int mem_1;
int mem_2;
int mem_3;
std::string str_mem;
Bar class_mem;
auto print() {
printf("[%d,%d,%d,%s,%d]\n", mem_1, mem_2, mem_3, str_mem.c_str(), class_mem.GetVal());
}
};
auto main(int argc, char** argv) -> int {
Foo foo{{"base1", 2}, 1, 2, 3}; // How `foo` is constructed, any constructor get called?
foo.print();
Foo foo1{{"base2", 2}, 1, 2, 3, "string", 3};
foo1.print();
}
这是aggregate initialization, Foo
is an aggregate type。
Each
direct public base, (since C++17)
array element, or non-static class member, in order of array subscript/appearance in the class definition, is copy-initialized from the corresponding clause of the initializer list.
给定 Foo foo1{{"base2", 2}, 1, 2, 3, "string", 3};
,Base
的子对象从 {"base2", 2}
初始化(它也得到 aggregate-initialized),数据成员 mem_1
从1
、mem_2
从2
初始化,mem_3
从3
初始化,str_mem
从"string"
初始化(通过std::string
的构造函数采用 const char*
),class_mem
从 3
初始化(由构造函数 Bar::Bar(int)
)。
给定Foo foo{{"base1", 2}, 1, 2, 3};
,除了str_mem
和class_mem
是从空列表初始化的,前面的成员的初始化方式与上面相同,结果是default-initialized(由它们的默认构造函数)。
If the number of initializer clauses is less than the number of members
and bases (since C++17)
or initializer list is completely empty, the remaining membersand bases (since C++17)
areinitialized by their default member initializers, if provided in the class definition, and otherwise (since C++14)
copy-initialized from empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates).