mardi 23 novembre 2021

How can I decide to better implement this functionality? [closed]

Tell me, please, how to properly organize this idea? I have library A and two projects B, C. Project B connects library A, and project C also connects library A.

I want to do something like this:

code of library A:

template<class TContainer> class a {
public:
   virtual void foo (const TContainer& container) = 0;
};

template<class TContainer> class b: public a<TContainer> {
public:
   virtual void foo (const TContainer& container) {
      // a lot of code
      int info = getInfoFromContainer(container);
      // a lot of code
   };
   int getInfoFromContainer (const TContainer& container) {
      return 0;
   };
};

template<class TContainer> class c : public a<TContainer> {
public:
   virtual void foo (const TContainer& container) {
      // a lot of code
      double info = getInfoFromContainer(container);
      // a lot of code
   };
   double getInfoFromContainer (const TContainer& container) {
      return 0;
   };
};

code of project B:

// only here the container implementation appears
class ContainerB;

// explicit specialization for ContainerB
template<> int b<ContainerB>::getInfoFromContainer (const ContainerB& container) { ... }
template<> double c<ContainerB>::getInfoFromContainer (const ContainerB& container) { ... }

// example using
ContainerB     myContainer;
a<ContainerB>* myClass = new b<ContainerB>();
myClass->foo(myContainer);

code of project C:

// only here the container implementation appears
class ContainerC;

// explicit specialization for ContainerC
template<> int b<ContainerC>::getInfoFromContainer (const ContainerC& container) { ... }
template<> double c<ContainerC>::getInfoFromContainer (const ContainerC& container) { ... }

// example using
ContainerC     myContainer;
a<ContainerC>* myClass = new b<ContainerC>();
myClass->foo(myContainer);

The problem is that it is "difficult" for containers B and C to create an abstract class. And declare this abstract class in library A. And also that in projects B and C, I must operate with a pointer to class a. Classes a, b, c were moved to library A to reduce the amount of repetitive code. Briefly, about the problem, you need getInfoFromContainer to take a different type and work out differently in projects B and C.

Is it possible to do this or is it bad to maintain in the future? How else can you do it? Is it possible to use a functional style here? Senc.

Aucun commentaire:

Enregistrer un commentaire