简体   繁体   中英

How to initialize static const char* enum type traits array?

I've encountered a header in an external sdk like so:

// external.h
//
template <class T> class MyBaseEnum
{
public: 
    /** String list. */
    static const char *mStrings[];

    //! Constructor.
    inline MyBaseEnum(){}  

    //! Destructor.
    inline ~MyBaseEnum()
    {

    }
};

I've seen this class used in the same sdk in another header like so:

// foo.h
//
class Foo
{
    enum MyEnum 
    {
        A = 0,
        B,
        C
    };
    typedef MyBaseEnum< MyEnum > MyEnumType;

    MyEnumType bar;
};

I don't have access to the corresponding cpp file to see how mStrings is initialized for bar, but I think this is related to type traits.

What would the syntax look like in foo.cpp to correctly initialize MyEnumType::mStrings ?

As MyBaseEnum is a templated class, the mStrings member can't be defined in a separate source file. It has to be done in a header file that is included, as the definition requires the template argument.

The syntax is basically the same as defining any other string array:

template<class T>
const char* MyBaseEnum<T>::mStrings = { ... };

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