struct BasePluginInfo
{
bool bHasGui, bIsSynth;
char cType;
std::string sCategory, sSdkVersion, sVendor, sVersion;
};
struct PluginClassInfo
{
std::string sName, sUid;
std::vector<std::string> vsParamNames;
};
struct ShellPluginInfo : BasePluginInfo
{
std::vector<PluginClassInfo> vciClasses;
};
When I do
ShellPluginInfo
{
.bHasGui = true
};
The compiler complains that ShellPluginInfo has no field 'bHasGui'
.
However this works:
ShellPluginInfo info;
info.bHasGui = true;
SO complains I have too much code, so these are a few words to fill it up.
When aggregate initializing something with base classes, the base class acts like a member of the class, similar to if you had:
struct ShellPluginInfo {
BasePluginInfo __base_class_subobject;
std::vector<PluginClassInfo> vciClasses;
};
As such, the first clause in the initializer list will try to initialize it, and you have to write:
ShellPluginInfo{ // This initializer list initializes a ShellPluginInfo
{ .bHasGui = true; } // This initializer list initializes a BasePluginInfo
}
However, since this is not a designated initializer, you cannot use designated initializers for the rest of the members of the derived class. For example:
ShellPluginInfo{
{ // (note: doesn't have a designator)
.bHasGui = true,
.cType = 'a' // OK
},
.vciClasses = {} // ERROR: Can't mix designated and non-designated initializers
}
This proposal attempts to remedy that: https://wg21.link/p2287r1 , making your original attempt valid. It has not been accepted yet though, but you may see it in C++23.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.