来自Aggregate initialization, set pointer to struct member ,以下代码合法吗:
struct S
{
int a;
int* aptr;
};
S s = { 3, &s.a };
最佳答案
引用最新标准草案:
[basic.scope.pdecl]
The point of declaration for a name is immediately after its complete declarator ([dcl.decl]) and before its initializer (if any), except as noted below.
所以,是的。标识符 s
已被声明,因此可以在其初始化程序中使用。
请注意,s
的值只有在初始化后才能使用。示例中未使用该值,因此这不是问题。
I'd also be curious about whether analogous code is valid when the two members of S are in reversed order
成员的顺序并不重要。
关于c++ - 对象初始化中是否允许指向 this 成员的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60191266/