使用polymoric方法从非模板基本案例派生模板类?

时间:2018-07-03 20:08:37

标签: c++ c++11 templates polymorphism

在C ++ 11中,模板类可以从具有相同方法签名的非模板基类派生其方法吗?示例:

void DoSomething() {return;}

class MyBase {
public:
    MyBase() {};
    virtual void Draw() {DoSomething();};
    virtual void Draw2() = 0;
}

template<int T>
class MyShape1<T> : public MyBase {
public:
    MyShape1() : MyBase() {}
    void Draw2();
};

class MySpape2() : public MyBase() {
    MyShape2() : MyBase() {}
    void Draw2();
};

template<int T>
void MyShape1<T>::MyDraw2() {
    DoSomething();
}

void MyShape2::MyDraw2() {
    DoSomething();
}

int main(int argn, char** argv) {
    MyShape1<5> X();
    MyShape2    Y();

    X.Draw();
    Y.Draw();

    MyBase* Z;

    Z = &X;
    Z->Draw2();

    Z = &Y;
    Z->Draw2();
    return 0;
}

0 个答案:

没有答案