简体   繁体   中英

Shortcut to nested type in C++ class

In a generated class I have an anonymous enum with many members, some with long names, like this:

class ContainerClass {
public:
  enum { Enum1, EnumWithLongName2 ...};
};

This is fine to express that these enums solely belong to that class. At times however I have a large list of these enums (eg in result lists for unit tests), which get very large when I have to qualify each enum with the class' name. Is there a way to add a shortcut in such cases so that I can omit the class name?

I looked at C++'s type alias , but that doesn't seem to help here. Other ideas?

Yes, there's one : a derived class inherits the enum names and can use them without further qualification.

So, create a helper class which inherits from ContainerClass to define the list. However, the helper class isn't intended to be instantiated (You wouldn't want to crate another ContainerClass base subobject) so define the list as a static member:

class enumList : public ContainerClass {
        enumList() = delete;
    public:
        static decltype(Enum1) list[2] = { Enum1, EnumWithLongName2 };
};

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM