A
alexander.stippler
Hello,
I need on the one hand static polymorphism for efficiency concerns,
e.g.
template <typename P>
class Problem
{
public:
const P &
problem() const { return static_cast<const P &>(*this); }
P &
problem() { return static_cast<P &>(*this); }
void
method1() { this->problem().method1(); }
// .... other methods
};
template <typename T>
class SpecificProblem
: public Problem<SpecificProblem<T> >
{
public:
void
method1() {}
};
There is one specific interface all derived classes have to share, so
this CRTP-method is well suited.
But I also want the following: Which specific problem I have to
instantiate is given by some external run-time value, let's say by a
string. Ok, so I could use the Factory pattern for instatiation. But
for this pattern I need some common base class as return type for the
created object. Let us for the moment consider we have this type
'ProblemType *' (it cannot be 'Problem *'!). There is no way back such
that I could use the structure above, i.e. call 'method1()' efficiently
(or at all). Any solutions.
I need:
several classes all sharing a common interface.
a method like factory pattern to instantiate the classes given a
'string value'
efficient usage of the class's methods afterwards (no virtual
function calls!)
I fear, that's too many requirements to fulfill at the same time.
Best regards,
Alex
I need on the one hand static polymorphism for efficiency concerns,
e.g.
template <typename P>
class Problem
{
public:
const P &
problem() const { return static_cast<const P &>(*this); }
P &
problem() { return static_cast<P &>(*this); }
void
method1() { this->problem().method1(); }
// .... other methods
};
template <typename T>
class SpecificProblem
: public Problem<SpecificProblem<T> >
{
public:
void
method1() {}
};
There is one specific interface all derived classes have to share, so
this CRTP-method is well suited.
But I also want the following: Which specific problem I have to
instantiate is given by some external run-time value, let's say by a
string. Ok, so I could use the Factory pattern for instatiation. But
for this pattern I need some common base class as return type for the
created object. Let us for the moment consider we have this type
'ProblemType *' (it cannot be 'Problem *'!). There is no way back such
that I could use the structure above, i.e. call 'method1()' efficiently
(or at all). Any solutions.
I need:
several classes all sharing a common interface.
a method like factory pattern to instantiate the classes given a
'string value'
efficient usage of the class's methods afterwards (no virtual
function calls!)
I fear, that's too many requirements to fulfill at the same time.
Best regards,
Alex