lundi 29 décembre 2014

Create type copies

How do you create type copies? For example, how do I create types Mass, Acceleration and Force which are not implicitly convertible to double(or any other numeric type), but otherwise have all the characteristics of a double. This would allow a compile-time input validity check for this function:



Force GetForceNeeded(Mass m, Acceleration a);


ensuring that GetForceNeeded can only be called with arguments of type Mass and Acceleration.


Of course, I could achieve this by manually creating a copy of the type:



class Force final
{
public:
//overload all operators
private:
double value;
};


but this is cumbersome. Is there a generic solution?


Aucun commentaire:

Enregistrer un commentaire