C++ 使用构造函数与大括号初始值设定项列表初始化类和结构的规则是什么?
我已经在网上搜索了这个问题的答案,但还没有找到满意的答案。我想知道结构和类类型的对象初始化的所有规则是什么,特别是当涉及到构造函数和带括号的初始值设定项列表时。结构和类的规则也不同吗 假设我们有一个名为C++ 使用构造函数与大括号初始值设定项列表初始化类和结构的规则是什么?,c++,c++11,constructor,initializer-list,list-initialization,C++,C++11,Constructor,Initializer List,List Initialization,我已经在网上搜索了这个问题的答案,但还没有找到满意的答案。我想知道结构和类类型的对象初始化的所有规则是什么,特别是当涉及到构造函数和带括号的初始值设定项列表时。结构和类的规则也不同吗 假设我们有一个名为Rectangle的类或结构 #include <iostream> using namespace std; class Rectangle { public: Rectangle() : x(5.0), y(6.0), width(7.0), height(8.0)
Rectangle
的类或结构
#include <iostream>
using namespace std;
class Rectangle {
public:
Rectangle() : x(5.0), y(6.0), width(7.0), height(8.0) {}
void printMe()
{
cout << "The rectangle is located at (" << x << ',' << y << ") and is " << width << " x " << height << endl;
}
double x;
double y;
double width;
double height;
};
int main()
{
Rectangle r = {0.0, 0.0, 3.0, 4.0};
r.printMe();
Rectangle s; // uninitialized!
s.printMe();
}
嗯。。。。乍一看,这不是很有用的错误消息。然而,我认为它与构造函数有关,因为如果我删除它,代码将编译并运行!我认为这是一个悖论,带括号的初始值设定项列表和构造函数似乎都在竞争初始化数据成员
但是,当我将数据成员设置为私有时,在删除构造函数(即)后,再次显示相同的错误消息
我想知道初始化数据成员的优先规则是什么。大括号初始值设定项列表与您自己定义的构造函数相比如何?它与C++11特性相比如何:=default
构造函数和类内成员初始值设定项?我假设这些初始化对象数据成员的不同方法会以某种方式相互冲突
Rectangle() = default;
...
double x = 1.0;
我并不是说把它们混在一起就一定是好代码,只是我认为应该很好地理解这些代码。谢谢。CPP标准草案n4713规定了有关聚合初始化的内容: 11.6.1聚合[dcl.init.aggr]
1聚合是具有
(1.1)-没有用户提供、显式或继承的构造函数,
(1.2)-无私有或受保护的非静态数据成员
在您的情况下,第一种情况下有一个用户提供的构造函数,第二种情况下有一个私有数据成员,这分别违反了上述(1.1)和(1.2)要点。CPP标准草案n4713规定了聚合初始化: 11.6.1聚合[dcl.init.aggr]
1聚合是具有
(1.1)-没有用户提供、显式或继承的构造函数,
(1.2)-无私有或受保护的非静态数据成员
在您的情况下,第一种情况下有一个用户提供的构造函数,第二种情况下有一个私有数据成员,这分别违反了上述(1.1)和(1.2)要点。下面是一个示例,演示了它们的区别。C++中的初始化相当复杂。请参阅: 通常最好使用或。您在构造函数中做的事情是正确的。只需使用或调用构造函数,以避免混淆人员。通常,您将只使用复制列表初始化来初始化聚合,而不使用用户提供的构造函数
#include <iostream>
struct A {
int i;
};
struct B {
B() = default;
int i;
};
struct C {
C();
int i;
};
C::C() = default;
struct D {
D(){};
int i;
};
struct E : public D {
};
struct F {
F(int i = 5) {}
int i;
};
struct G {
G() = delete;
int i;
};
int main() {
// g++ (v 8.2.1) provides good warnings about uninitialized values.
// clang++ (v 7.0.1) does not.
// Technically, they are initialized to 'indeterminate values', but it is
// easier to refer to the member variables as uninitialized.
{
// All of the following are 'default initialized', meaning they are not even
// zero-initialized. Members are UNINITIALIZED (Technically, they are
// initialized to 'indeterminate' values.
// Either nothing is done, or the default constructor is called (in
// which nothing is done).
A a;
B b;
C c;
D d;
E e;
F f;
std::cout << "a: " << a.i << std::endl;
std::cout << "b: " << b.i << std::endl;
std::cout << "c: " << c.i << std::endl;
std::cout << "d: " << d.i << std::endl;
std::cout << "e: " << e.i << std::endl;
std::cout << "f: " << f.i << std::endl;
std::cout << std::endl;
} {
// This is more complex, as these are all 'list initialized'.
// Thank you, infinite wisdom of the C++ committee.
A a{};
// Direct list initialization -> aggregate initialization
// - A has no user-provided constructor and
// thus is an aggregate, and agg. init. takes place.
// This 'value initializes' all *MEMBERS* (unless a default member
// initializer exists, which it does not here).
// Value initialization of non-class types results in
// zero-initialization. (member `i` is zero-initialized)
A a2 = {};
// same thing, but via copy list initialization
A a3{{}};
// recursive, initializes `i` with {}, which zero initializes `i`.
A a4{0};
// recursive, initializes `i` 0;
// Could also do `A a4 = {0}`
A a5{a};
// direct intialization of `a5` with `a`.
// Implicit copy constructor chosen by overload resolution.
A a6{A{}};
// post C++17, direct initializes a6 with a prvalue of type A, that is
// aggregate initialized as above. NOT copy/move initialized, but
// instead initialized via the "initializer expression itself".
// I assume this means the value of a6 is directly set via as if it were
// being aggregate initialized.
B b{};
// Same as A. `B() = default;` does NOT specify a user-provided
// constructor
C c{};
// Because the first declaration of `C()` is not `C() = default;`,
// this DOES have a user-provided constructor, and 'value initializaton'
// is performed.
// NOTE: this value intializes `C`, not the *MEMBERS* of `C`.
// Because `C` is a normal class type, value initialization just calls
// the default constructor, which does nothing, and leaves all members
// uninitialized.
D d{};
// D is a class type that is list/direct initialization -> value
// inititalizaton -> default initialization -> call constructor ->
// members are left unitialized.
E e{};
// List initialization -> value initialization -> default initialization
// -> calls implicitly defined default constructor -> Calls default
// constructor of bases -> leaves E::D.i uninitialized
F f{};
// List/direct initialization -> value initialization -> calls default
// constructor with default arguments -> leaves F.i uninitialized
// G g{};
// Fails to compile.
// list initialization -> value initialization -> default initialization
// -> deleted default constructor selected by overload resolution ->
// fails to compile
std::cout << "a: " << a.i << std::endl;
std::cout << "a2: " << a2.i << std::endl;
std::cout << "a3: " << a3.i << std::endl;
std::cout << "a4: " << a4.i << std::endl;
std::cout << "a5: " << a5.i << std::endl;
std::cout << "a6: " << a6.i << std::endl;
std::cout << "b: " << b.i << std::endl;
std::cout << "c: " << c.i << std::endl;
std::cout << "d: " << d.i << std::endl;
std::cout << "e: " << e.i << std::endl;
std::cout << "f: " << f.i << std::endl;
}
}
#包括
结构A{
int i;
};
结构B{
B()=默认值;
int i;
};
结构C{
C();
int i;
};
C::C()=默认值;
结构D{
D(){};
int i;
};
结构E:公共D{
};
结构F{
F(int i=5){}
int i;
};
结构G{
G()=删除;
int i;
};
int main(){
//g++(v8.2.1)提供了关于未初始化值的良好警告。
//clang++(V7.0.1)没有。
//从技术上讲,它们被初始化为“不确定值”,但实际上是
//更容易将成员变量称为未初始化。
{
//以下所有内容都是“默认初始化”,这意味着它们不是偶数
//零初始化。成员未初始化(从技术上讲,它们是
//初始化为“不确定”值。
//要么什么都不做,要么调用默认构造函数(在
//什么也没做)。
A A;
B B;
C C;
D;
E;
F;
STD::CUT这里展示了一个例子。C++中的初始化非常复杂。参见:/P>
通常最好使用or。您在构造函数中做的事情是正确的。只需使用or调用构造函数,以避免混淆人员。通常,您只使用复制列表初始化来初始化聚合,而不使用用户提供的构造函数
#include <iostream>
struct A {
int i;
};
struct B {
B() = default;
int i;
};
struct C {
C();
int i;
};
C::C() = default;
struct D {
D(){};
int i;
};
struct E : public D {
};
struct F {
F(int i = 5) {}
int i;
};
struct G {
G() = delete;
int i;
};
int main() {
// g++ (v 8.2.1) provides good warnings about uninitialized values.
// clang++ (v 7.0.1) does not.
// Technically, they are initialized to 'indeterminate values', but it is
// easier to refer to the member variables as uninitialized.
{
// All of the following are 'default initialized', meaning they are not even
// zero-initialized. Members are UNINITIALIZED (Technically, they are
// initialized to 'indeterminate' values.
// Either nothing is done, or the default constructor is called (in
// which nothing is done).
A a;
B b;
C c;
D d;
E e;
F f;
std::cout << "a: " << a.i << std::endl;
std::cout << "b: " << b.i << std::endl;
std::cout << "c: " << c.i << std::endl;
std::cout << "d: " << d.i << std::endl;
std::cout << "e: " << e.i << std::endl;
std::cout << "f: " << f.i << std::endl;
std::cout << std::endl;
} {
// This is more complex, as these are all 'list initialized'.
// Thank you, infinite wisdom of the C++ committee.
A a{};
// Direct list initialization -> aggregate initialization
// - A has no user-provided constructor and
// thus is an aggregate, and agg. init. takes place.
// This 'value initializes' all *MEMBERS* (unless a default member
// initializer exists, which it does not here).
// Value initialization of non-class types results in
// zero-initialization. (member `i` is zero-initialized)
A a2 = {};
// same thing, but via copy list initialization
A a3{{}};
// recursive, initializes `i` with {}, which zero initializes `i`.
A a4{0};
// recursive, initializes `i` 0;
// Could also do `A a4 = {0}`
A a5{a};
// direct intialization of `a5` with `a`.
// Implicit copy constructor chosen by overload resolution.
A a6{A{}};
// post C++17, direct initializes a6 with a prvalue of type A, that is
// aggregate initialized as above. NOT copy/move initialized, but
// instead initialized via the "initializer expression itself".
// I assume this means the value of a6 is directly set via as if it were
// being aggregate initialized.
B b{};
// Same as A. `B() = default;` does NOT specify a user-provided
// constructor
C c{};
// Because the first declaration of `C()` is not `C() = default;`,
// this DOES have a user-provided constructor, and 'value initializaton'
// is performed.
// NOTE: this value intializes `C`, not the *MEMBERS* of `C`.
// Because `C` is a normal class type, value initialization just calls
// the default constructor, which does nothing, and leaves all members
// uninitialized.
D d{};
// D is a class type that is list/direct initialization -> value
// inititalizaton -> default initialization -> call constructor ->
// members are left unitialized.
E e{};
// List initialization -> value initialization -> default initialization
// -> calls implicitly defined default constructor -> Calls default
// constructor of bases -> leaves E::D.i uninitialized
F f{};
// List/direct initialization -> value initialization -> calls default
// constructor with default arguments -> leaves F.i uninitialized
// G g{};
// Fails to compile.
// list initialization -> value initialization -> default initialization
// -> deleted default constructor selected by overload resolution ->
// fails to compile
std::cout << "a: " << a.i << std::endl;
std::cout << "a2: " << a2.i << std::endl;
std::cout << "a3: " << a3.i << std::endl;
std::cout << "a4: " << a4.i << std::endl;
std::cout << "a5: " << a5.i << std::endl;
std::cout << "a6: " << a6.i << std::endl;
std::cout << "b: " << b.i << std::endl;
std::cout << "c: " << c.i << std::endl;
std::cout << "d: " << d.i << std::endl;
std::cout << "e: " << e.i << std::endl;
std::cout << "f: " << f.i << std::endl;
}
}
#包括
结构A{
int i;
};
结构B{
B()=默认值;
int i;
};
结构C{
C();
int i;
};
C::C()=默认值;
结构D{
D(){};
int i;
};
结构E:公共D{
};
结构F{
F(int i=5){}
int i;
};
结构G{
G()=删除;
int i;
};
int main(){
//g++(v8.2.1)提供了关于未初始化值的良好警告。
//clang++(V7.0.1)没有。
//从技术上讲,它们被初始化为“不确定值”,但实际上是
//更容易将成员变量称为未初始化。
{
//以下所有内容都是“默认初始化”,这意味着它们不是偶数
//零初始化。成员未初始化(从技术上讲,它们是
//初始化为“不确定”值。
//要么什么都不做,要么调用默认构造函数(在
//什么也没做)。
A A;
B B;
C C;
D;
E;
F;
std::您是否正在尝试使用,但由于您给了类一个用户定义的构造函数,它不再是聚合,无法通过聚合初始化来构造。具有私有成员的类也不是聚合。@IgorTandetnik写一个答案!您正在尝试使用,但由于您给了类一个用户定义的构造函数,它是no不再是聚合,无法通过聚合初始化来构造。具有私有成员的类也不是聚合。@IgorTandetnik写一个答案!您的示例是一个很好的演示。您的示例是一个很好的演示。