[英]Using Designated Initializer on member of inherited base class
I created a class Something
which holds a member variable.我创建了一个包含成员变量的类Something
。
I am able to do this, which works as intended:我能够做到这一点,它按预期工作:
struct Something
{
bool Works;
};
int main()
{
Something s = {
.Works = false,
};
}
Now, I want to created a struct inherited from Something
, named Something2
, and want to do the same thing, but I want to be able to have access to Something::Works
(which I renamed DoesntWork
, since it doesn't work!):现在,我想创建一个继承自Something
的结构,命名为Something2
,并想做同样的事情,但我希望能够访问Something::Works
(我将其重命名为DoesntWork
,因为它不起作用! ):
struct Something
{
bool DoesntWork;
};
struct Something2 : public Something
{
bool Works;
};
int main()
{
Something2 s2 = {
.Works = true,
.DoesntWork = false,
};
}
https://godbolt.org/z/rYT3br467 https://godbolt.org/z/rYT3br467
I am not looking for an alternative, I can already think of some.我不是在寻找替代方案,我已经可以想到一些。 I am only wondering if this is possible, and if yes then how.我只是想知道这是否可能,如果是,那么如何。
If it is not possible, I would really like to know the low-level reason, so don't hesitate!如果不可能,我很想知道低级原因,所以不要犹豫!
UPDATE: Thanks to Nathan Oliver's comment, you can read: Designated initializers in C++20更新:感谢 Nathan Oliver 的评论,您可以阅读: C++20 中的指定初始化程序
C++ is much stricter when it comes to designated initializers than C.在指定初始化器方面,C++比C 严格得多。
cppreference : 参考:
out-of-order designated initialization, nested designated initialization , mixing of designated initializers and regular initializers , and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++ .乱序指定初始化、嵌套指定初始化、指定初始化和常规初始化混合、数组的指定初始化在C编程语言中都是支持的,但在C++中是不允许的。
C doesn't have the inheritance concept at all which makes C's version of designating (at least untill C++20) impossible. C 根本没有继承概念,这使得 C 的指定版本(至少在 C++20 之前)是不可能的。 One possible, future, version would be to designate it "forward" as often done in constructors with delegating constructors.一种可能的未来版本是在具有委托构造函数的构造函数中将其指定为“向前”。 In that case, I'd expect the proper initialization to be this:在这种情况下,我希望正确的初始化是这样的:
Something2 s2 = {
Something = {
.Works = false
},
.DoesntWorks = true
};
That's however not part of C++ as of C++20.然而,从 C++20 开始,这不是 C++ 的一部分。 You can however create constructors in C++ to accommodate (part of) your needs without designated initializers:但是,您可以在 C++ 中创建构造函数来满足(部分)您的需求,而无需指定初始值设定项:
struct Something {
bool DoesntWork;
};
struct Something2 : public Something {
Something2(bool Doesnt, bool Does) :
Something{Doesnt},
Works{Does}
{}
bool Works;
};
int main() {
Something2 s2
{
true, false
};
}
or initialize each object without user defined constructors at all:或者在没有用户定义的构造函数的情况下初始化每个对象:
struct Something {
bool DoesntWork;
};
struct Something2 : public Something {
bool Works;
};
int main() {
Something2 s2{
// You could just make it `{true}` below, but mentioning the
// subobject fits the purpose of a designated initializer for clarity:
Something{true},
// this must however be left unmentioned in C++20 since you can't
// mix regular initializers with designated ones:
false
};
}
This works for me:这对我有用:
struct Something
{
bool ThisWorks;
};
struct Something2 : Something
{
bool Works;
};
int main()
{
Something2 s2 = {
{ .ThisWorks = false },
.Works = true
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.