samedi 29 février 2020

How to wrap an object and its member functions with c++

I'm developing a c++ class to wrap an object and its member functions. As the object may be changed, so I'm thinking if it is possible to use some mechanism to do so, such as template. Here is an example:

class ObjectA
{
public:
    void funcA1(double);
    void funcA2(int, char);
private:
    int value;
};

class ObjectB
{
public:
    void funcB1(double);
    void funcB2(int, char);
private:
    int value;
    float point;
};

Now I need to develop a class, which can easily wrap the ObjectA or the ObjectB. It is for sure that ObjectA and ObjectB don't inherit any parent class and they must have two functions, the names of functions are not the same but the parameters must be the same, just like funcA1 funcB1 and funcA2 funcB2.

I'm thinking the code as below should work.

template<typename T, typename funcAT, typename funcBT> // T: Object, funcAT: Object::funcA, funcBT: Object::funcB
class Wrapper
{
    // ...
};

Well, if the code works, is there some method to make more constraint? For example, how to make sure that funcAT must belong to the T, instead of other functions? how to make sure that funcAT must have only one parameter double, instead of other cases?

What I mean is:

ObjectA objA;
Wrapper<objA, objA::funcA1, objA::funcB1> wrapper; // no error
Wrapper<objA, other_funcA, other_funcB> wrapper; // ERROR

Aucun commentaire:

Enregistrer un commentaire