C++ class pointer without giving template parameters

后端 未结 3 1420
生来不讨喜
生来不讨喜 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条回答
  •  -上瘾入骨i
    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 X : public Interface
    {
        //...
        virtual void doSomething() override;
    };
    
    std::unique_ptr myClass;
    
    //....
    myClass.reset(new X());
    myClass->doSomething();
    

提交回复
热议问题