lundi 25 novembre 2019

Template functions by index in C++

Suppose I have a class in C++11 like this:

class Something
{
...
private:
   class1* a;
   class2* b;
   class3* c;

public:

   class1* reada() { return a; }
   class2* readb() { return b; }
   class3* readc() { return c; }

   void customFunctionForclass1();
   void customFunctionForclass2();
   void customFunctionForclass3();
}


}

I'd like to make the read functions templated so that if another programmer adds another member class, the corresponding read function will be template-magic created.

Something like this maybe?

class Something
{
...
private:

   templateContainer = {class1*,class2*,class3*}

   template<thing in templateContainer>
   thing variableOfTypeThing;

public:

   template<thing in templateContainer>
   <thing> read() {return variableOfTypeThing<thing>;}

   void customFunctionForclass1();
   void customFunctionForclass2();
   void customFunctionForclass3();
}

As you can tell from the example, I'm confused.

Basically, I have a class which acts as a container for guaranteed unique class variables (no class1 A; class1 B) Some function groups for the class are almost identical some function groups are highly varied. It would be great for future people to only have to modify the different parts of the class and get the rest from the templates.

I thought maybe there would be a way by splitting this class up into lots of classes and stuffing them into an array of void pointers, but that seems unwise.

Suggestions?

Aucun commentaire:

Enregistrer un commentaire