简体   繁体   English

在继承的基类的成员上使用指定初始化器

[英]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
    };
}

Online Demo在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 派生类:在初始化列表中使用基类成员 - Derived class: using Base class member in initializer list 使用从模板化基类继承的成员变量(C ++) - Using member variables inherited from a templated base class (C++) 使用基类指针访问受保护的继承成员 - access protected inherited member with pointer to base class 将构造函数库和成员初始化器列表与继承一起使用 - Using constructor base and member initializer list with inheritance 初始化任何联合成员(指定初始化器) - Initialize Any Union Member (designated initializer) 构造函数干扰成员变量指定的初始化程序? - Constructor interferes with member variable designated initializer? 当初始值设定项是基类名称时出现错误“初始值设定项未命名非静态数据成员或基类” - Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name 使用不同的基值 class 初始化继承的 class 中的成员 - Initializing a member in an inherited class with different value of base class 向上转换到基础 class 时如何使用继承的 class 成员的值 - How to use value of inherited class member, when upcasting to base class 初始化列表:基类的构造函数和成员函数 - initializer list: a constructor from the base class and a member function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM