Simplified I have the following class hierarchy:
class BaseVec {
public:
BaseVec() {};
virtual ~BaseVec() {};
virtual double get_double(int i) con
Automatic dispatch in C++ happens with runtime-polymorphism by means of virtua function and with static_type polymorphism by mnas of a static_Cast, but you need to know what type to cast.
With a different design, avoiding void*
, you can do the following:
template
class static_polimorphic {};
template
A& upcast(static_polymorphic& sa)
{ return static_cast(sa); }
template
const A& upcast(const static_polymorphic& sa)
{ return static_cast(sa); }
Now, you classes shold be like
class C1: public static_polymorphic
{
....
};
class C2: public static_polymorphic
{
....
};
Polymorphism will then apply as
template
void function(const static_polymorphic& sa)
{
A& a = upcast(sa);
a.methods();
...
}
In other words, the type is anymore represented a base member variable, but by a base template parameter.
Note also that being the bases differentiated by the derived type, common functions will not reaqire to be virtual. You can so completely avoid runtimes-based polymorphism, unless you have to store different runtime-type created object into a same container or collection.
For that purpose you can use a second non-tempetised base with abstract virtual function as "launchers" for the one in the derived classes. (May be better to use the runtime polymorphic one as first base, to simplify run-time pointer convertion, since there will be no offset)