mercredi 21 octobre 2020

Is it ok to have Interface with void* as arguments? what is a better practice?

I wanted to have an abstraction between two classes and need to pass data between them, at the interface level we are not aware of the type of data, but the concreate implementation of the interfaces are aware of the data type, and they are different for each concreate implementation. below is the example:

class IA
{
    void func(void* arg);
};

class IB
{
  void setData(TypeA arg)
  {
    pA->func(reinterpret_cast<void*>(&arg));
  }

private:
IA* pA;
};

I want to avoid passing void* data across objects, but this would lead to implementation of several func() for each data type. and breaks the implementation for interfaces (Substitution Principle).

Thanks for you suggestions.

Aucun commentaire:

Enregistrer un commentaire