mardi 3 juillet 2018

Deriving template-class from non-template base case with polymoric methods?

in C++11 can a template class derive its methods from a non-template base class that have the same method signature? Example:

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;
}

Aucun commentaire:

Enregistrer un commentaire