dimanche 6 août 2017

constructor that calls other constructors with different signatures

Let's say i have a class called Shape and in Shape i have different static methods like
static Shape makeTriangle() with some parameters.
Then i have...
static Shape makeSquare() with different type and number of parameters than makeTriangle
then
static Shape makeSphere()
with exactly the same type and number of parameters than makeTriangle and so on

How can i create a constructor that takes an std::string as it's first parameter and a variable number of parameters and calls the right "make" method that creates the shape object?
Is this possible in c++11?

I tried using variadic templates but at some point i have to do something like
if(nameString == "box") return makeBox(/*unpack parameter pack here*/)
if(nameString == "sphere") return makeSphere(/*unpack parameter pack here*/)
which the compiler would refuse to compile because the parameter pack might not match some make method parameters. I cannot put it in a map that takes a string and outputs a function pointer either since the mentioned make methods have different signatures.
So again how can i do this in c++11?

Aucun commentaire:

Enregistrer un commentaire