jeudi 29 mars 2018

How to write a class that can be derived from any of the other derived classes of the same base class

I am not even sure if this can be done. However, this is the situation at hand.

I have a base class:

class Base {
public:
    virtual void fun();
private:
    int variable;
}

Consider that I have two derived classes:

class Derived1 : public Base {
public:
    virtual void fun() override;
    virtual void moreFun();
private:
    int variable;
}

and

class Derived2 : public Base {
public:
    virtual void fun() override;
    virtual void moreFun();
private:
    int variable;
}

I want to write a class Derived3 which can be derived either from Derived1 or Derived2.

The idea is to have Derived3 extend certain functionalities on top of one of the other derived classes. I don't intend to switch the class in runtime, but would like to initialize in a way similar to templates.

Template <class T>
class Derived3 : public T {
public:
    virtual void moreFun() override;
private:
    int variable;
}

Is this possible? Any other suggestions on how to approach this?

Aucun commentaire:

Enregistrer un commentaire