Using an enumeration as a template parameter

后端 未结 4 713
情书的邮戳
情书的邮戳 2020-12-13 09:29

I would like to use a template class to provide some common functionality to some child classes that are very similar. The only variation is the enumeration that each uses.

4条回答
  •  鱼传尺愫
    2020-12-13 09:59

    Enumerations can be template parameters in exactly the same way that ints can.

    enum Enum { ALPHA, BETA };
    
    template  class Foo {
        // ...
    };
    
    template <> void Foo  :: foo () {
        // specialise
    }
    
    class Bar : public Foo  {
        // OK
    }
    

    But you simply haven't provided a definition for E_EnumerationBase::E_EnumerationBase()

    This isn't a problem with templates or inheritence. It's the same as if you written this:

    struct Foo {
        Foo ();
    }
    int main () {
        Foo foo;
    }
    

提交回复
热议问题