是否可以在c++中初始化结构,如下所示:

struct address {
    int street_no;
    char *street_name;
    char *city;
    char *prov;
    char *postal_code;
};

address temp_address = { .city = "Hamilton", .prov = "Ontario" };

这里和这里的链接提到,这种样式只能在C中使用。如果是这样,为什么在c++中不能使用呢?是否有任何潜在的技术原因,为什么它不是在c++中实现的,或者使用这种风格是不好的做法。我喜欢使用这种初始化方式,因为我的结构体很大,而且这种样式可以让我清楚地了解分配给哪个成员的值。

请与我分享是否有其他方法可以达到同样的可读性。

在提出这个问题之前,我已参考以下连结:

C/ c++ for AIX C结构初始化变量 c++中使用标记的静态结构初始化 c++ 11正确的结构初始化


当前回答

你有

The standard initialization list address temp_address { /* street_no */, /* street_name */, ... /* postal_code */ }; address temp_address2 = { /* street_no */, /* street_name */, ... /* postal_code */ } The dot notation address temp_address; temp_address.street_no = ...; temp_address.street_name = ...; ... temp_address.postal_code = ...; The designated aggregate initialization, where the initialization list contains that labels of each member of the structure (see documentation) available from C++20 onward. Treating a struct like a C++ class - in C++ structures are actually special types of classes, where all members are public (unlike a standard C++ class where all members are private if not specified otherwise explicitly) as well as that when using inheritance they default to public: struct Address { int street_no; ... char* postal_code; Address (int _street_no, ... , char* _postal_code) : street_no(_street_no), ... postal_code(_postal_code) {} } ... Address temp_address ( /* street_no */, ..., /* postal_code */);

当涉及到初始化结构的方式时,你应该考虑以下方面:

Portability - different compilers, different degree of C++ standard completeness and different C++ standards altogether do limit your options. If you have to work with let's say a C++11 compiler but want to use the C++20 designated aggregate initialization you are out of luck Readability - what is more readable: temp_address.city = "Toronto" or temp_address { ..., "Toronto", ... }? Readability of your code is very important. Especially when you have large structures (worse - nested ones), having unlabeled values all over the place is just asking for trouble Scalability - anything that depends on a specific order is not a good idea. The same goes for lack of labels. You want to move a member up or down the address space of the structure? Good luck with an unlabeled initialization list (hunting down swapped values in structure initialization is a nightmare)... You want to add a new member? Again good luck with anything that depends on a specific order.

虽然点表示法意味着你输入更多,但你从使用它中得到的好处超过了这个问题,因此我建议你使用它,除非你有一个小的结构,它的结构缺乏变化,在这种情况下,你可以使用一个初始化列表。记住:无论何时与他人合作,编写易于遵循的代码都是至关重要的。

其他回答

受到这个非常简洁的答案的启发:(https://stackoverflow.com/a/49572324/4808079)

你可以使用lamba闭包:

// Nobody wants to remember the order of these things
struct SomeBigStruct {
  int min = 1;
  int mean = 3 ;
  int mode = 5;
  int max = 10;
  string name;
  string nickname;
  ... // the list goes on
}

.

class SomeClass {
  static const inline SomeBigStruct voiceAmps = []{
    ModulationTarget $ {};
    $.min = 0;  
    $.nickname = "Bobby";
    $.bloodtype = "O-";
    return $;
  }();
}

或者,如果你想要非常花哨的话

#define DesignatedInit(T, ...)\
  []{ T ${}; __VA_ARGS__; return $; }()

class SomeClass {
  static const inline SomeBigStruct voiceAmps = DesignatedInit(
    ModulationTarget,
    $.min = 0,
    $.nickname = "Bobby",
    $.bloodtype = "O-",
  );
}

这样做有一些缺点,主要与未初始化的成员有关。从链接的答案评论说,它编译有效,虽然我没有测试它。

总的来说,我认为这是一个很好的方法。

正如其他人提到的,这是指定初始化项。

这个特性是c++ 20的一部分

在gnuc++中(似乎从2.5开始就过时了,很久以前了:)看到答案在这里:使用标签初始化C结构。它可以工作,但如何?),可以像这样初始化一个结构体:

struct inventory_item {
    int bananas;
    int apples;
    int pineapples;
};

inventory_item first_item = {
    bananas: 2,
    apples: 49,
    pineapples: 4
};

你有

The standard initialization list address temp_address { /* street_no */, /* street_name */, ... /* postal_code */ }; address temp_address2 = { /* street_no */, /* street_name */, ... /* postal_code */ } The dot notation address temp_address; temp_address.street_no = ...; temp_address.street_name = ...; ... temp_address.postal_code = ...; The designated aggregate initialization, where the initialization list contains that labels of each member of the structure (see documentation) available from C++20 onward. Treating a struct like a C++ class - in C++ structures are actually special types of classes, where all members are public (unlike a standard C++ class where all members are private if not specified otherwise explicitly) as well as that when using inheritance they default to public: struct Address { int street_no; ... char* postal_code; Address (int _street_no, ... , char* _postal_code) : street_no(_street_no), ... postal_code(_postal_code) {} } ... Address temp_address ( /* street_no */, ..., /* postal_code */);

当涉及到初始化结构的方式时,你应该考虑以下方面:

Portability - different compilers, different degree of C++ standard completeness and different C++ standards altogether do limit your options. If you have to work with let's say a C++11 compiler but want to use the C++20 designated aggregate initialization you are out of luck Readability - what is more readable: temp_address.city = "Toronto" or temp_address { ..., "Toronto", ... }? Readability of your code is very important. Especially when you have large structures (worse - nested ones), having unlabeled values all over the place is just asking for trouble Scalability - anything that depends on a specific order is not a good idea. The same goes for lack of labels. You want to move a member up or down the address space of the structure? Good luck with an unlabeled initialization list (hunting down swapped values in structure initialization is a nightmare)... You want to add a new member? Again good luck with anything that depends on a specific order.

虽然点表示法意味着你输入更多,但你从使用它中得到的好处超过了这个问题,因此我建议你使用它,除非你有一个小的结构,它的结构缺乏变化,在这种情况下,你可以使用一个初始化列表。记住:无论何时与他人合作,编写易于遵循的代码都是至关重要的。

这是可能的,但前提是初始化的结构体是POD(普通旧数据)结构体。它不能包含任何方法、构造函数,甚至默认值。