mercredi 3 janvier 2018

Calling different methods with the same signature using a template like mechanism

I have this code:

struct C
{
   int d1;
   int d2;
};

struct A
{
      void  write(C data)
      {
      }
};

struct B
{
      void use(C data)
      {
      }
};

now I want to define a new class that uses A or B and call thier write and use method. Something such as this:

template <class T>
struct D
{
     void myFunct(C data)
     {
         T.????(data);
     }
};

As you can see if the two class has similar method names, then it was easy to implement D, but since A and B has different methods, then I need to tell compiler which method of these classes it should use. How can I do this?

Please note that: I don't want to change the format of the A and B and also I don't want to create a subclass of A and B to create a method with the same name.

I want a way to tell compiler as part of template which method to use. Is it possible?

I am using a compiler that support C++11.

Aucun commentaire:

Enregistrer un commentaire