mercredi 30 novembre 2016

How to automatically call a method or generate code if a subclass derived from a base class?

I have some classes that describe abilities / behaviours, such as flying, or driving etc. Each of these classes has a specific method that must be called to load some data - For example, Flyable has loadFlyData, Drivable has loadDriveData. For each class the method name is unique.

I have many derived classes that may inherit from one or more of these behaviour classes. Each of these derived classes has a method called loadData, in which we should call all the parent behaviour classes methods such as loadFlyData, loadDriveData etc.... Is there a way to automatically generate this method using metaprogramming ? Since there are many derived classes, it may be more maintainable if I can generate these methods using metaprogramming...

Behaviour classes : (An object class may have any of these behaviours, and will have to call that classes "load" method...

class Flyable{

  void loadFlyData(){
  }
};

class Drivable{
  void loadDriveData(){
  }
};

All object classes derive from Object:

class Object{
  virtual void loadData(){
  }
};

A derived class:

class FlyingCar : public Object, public Flyable, public Drivable{
  virtual void loadData()override{
        //How to automatically generate code so that the next two lines are called:
    loadFlyData();
    loadDriveData();
  }
};

Aucun commentaire:

Enregistrer un commentaire