C++ class pointer without giving template parameters

后端 未结 3 1418
生来不讨喜
生来不讨喜 2021-01-19 17:40

Hello dear people of the underworld called the internet.

Lets say we have a class called X with the template parameters(Y):

template
c         


        
相关标签:
3条回答
  • 2021-01-19 17:46

    X is not a type without its template argument so no, unfortunately not. You could achieve what you want if X had a base class which defined the interface you wanted to use though.

    For example,

    struct Interface
    {
        Interface() {}
        virtual ~Interface(){}
    
        virtual void doSomething() = 0;
    };
    
    template <class Y>
    class X : public Interface
    {
        //...
        virtual void doSomething() override;
    };
    
    std::unique_ptr<Interface> myClass;
    
    //....
    myClass.reset(new X<variable>());
    myClass->doSomething();
    
    0 讨论(0)
  • 2021-01-19 17:56

    No. A pointer points to a type, and X is not a type.

    0 讨论(0)
  • 2021-01-19 18:03

    Not with X*. Consider this alternative:

    class BaseX { 
        //...
    };
    
    template<class Y>
    class X : public BaseX
    {
        //...
    };
    

    Since BaseX is a complete type, you can have BaseX* that references some X<Y> once you've defined it.

    0 讨论(0)
提交回复
热议问题