如何在C 类的初始化器列表中使用未命名结构初始化成员结构
How to initialize member-struct with unnamed structure in initializer list of C++ class?
i具有内部未命名结构的结构。我想初始化整个结构,并且它的成员结构在类初始化器列表中。
struct Foo {
int z;
struct {
double upper;
double lower;
} x, y;
};
class Bar {
Bar();
Foo foo;
};
可以做到吗?
此结构还可以初始化"旧时尚"方式提供没有统一初始化语法的构造函数?
struct Foo {
Foo() : z(2), x(/*?*/), y(/*?*/) {}
Foo() : z(2), x.lower(2) {} // doesn't compile
int z;
struct {
double upper;
double lower;
} x, y;
};
如果我正确理解您,请在Bar
的初始化器列表中初始化包含未命名struct 的struct Foo
:
#include <iostream>
struct Foo {
int z;
struct {
double upper;
double lower;
} x, y;
};
class Bar {
public:
Bar();
Foo foo;
};
Bar::Bar()
: foo { 1, { 2.2, 3.3}, {4.4, 5.5} }
{
}
int main()
{
Bar b;
std::cout << b.foo.z << std::endl;
std::cout << b.foo.x.upper << std::endl;
std::cout << b.foo.y.lower << std::endl;
}
如果我正确理解,您想对完整结构的静态初始化,包括内在 nmind nameed struct。
您是否尝试过:
Foo foo { 1, // z
{1.1, 2.2}, // x
{3.3, 4.4}}; // y
相关文章:
- 在C和C++中初始化结构中的数组
- 我们可以用参数化构造函数初始化结构的数组吗?
- 尝试初始化结构内的联合时出错
- 使用其他成员初始化结构的成员?
- 使用构造函数初始化结构还是在之后设置其值更好?
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 如何在另一个结构中初始化结构数组?
- 如何在构造函数中初始化结构体的动态数组?
- 初始化结构中的联合 - c++
- 尝试初始化结构向量时出现编译错误
- 用于初始化结构的 void 指针强制转换
- 初始化结构的两种方式
- 使用用户定义的构造函数初始化C++结构
- 使用初始值设定项列表初始化结构,对于 std::array 失败
- 在 c++ 中初始化结构的指针
- 无序列图会创建一个零初始化结构吗?
- 如何在C++中初始化结构
- C 用结构指针初始化结构
- 在C 中初始化结构数组
- 使用数组聚合初始化结构数组