mardi 28 février 2017

Encapsulate a function with another function that has the same set of parameters

I have a function that has a lot of parameters. (4-7 parameters)

For simplicity, this is an example:-

class B{
    friend class C;
    int f(int param1,float param2, structA a, structB b){
         //... some code ...
    }
    //.... other functions ....
};

Sometimes, I want to encapsulate it under another (more-public) function that has the same signature:-

class C{
    B* b;
    public: int g(int param1,float param2, structA a, structB b){
        return b->f(param1,param2,a,b);
    }
    //.... other functions ....
};

In my opinion, the above code is :-

  • tedious
  • causes a bit of maintainability problem
  • human error-prone

Is there any C++ technique / magic / design-pattern to assist it?

In the real case, it happens mostly in edge-cases that composition is just a little more suitable than inheritance.

I feel that <...> might solve my problem, but it requires template from which I want to avoid.

Aucun commentaire:

Enregistrer un commentaire