How to design a vector class to use any sqrt function?

Started by
23 comments, last by johnstanp 15 years, 9 months ago
Quote:Original post by the_edd
I don't know what you mean by "passing a pointer". There are no pointers involved in the code I posted. All functions are class-scope static.


I was thinking of the other possibility of adding a template parameter, a pointer to a function. I was not referring to your code, but talking of the benefit of using it.

Quote:
By default std::sqrt is used (as it's called by the primary template). It does exactly what you want.


I understand that, but what if the functions I want to use, use the same type as the primary template?

[Edited by - johnstanp on July 9, 2008 4:44:50 PM]
Advertisement
The template suggestion I made above was before the OP edited the post. Templated calculation of values doesn't apply in this case.

The simplest solution still remains using templates and default arguments as was mentioned before:
template < class T >struct math_traits {  Error - unspecialized // faulty struct definition to fail};template <>struct math_traits<float> {  static float sqrt(float value);};template <>struct math_traits<int> {  static int sqrt(int value);};template < class T, class Math = math_traits<T> >struct Vector3D {  T length() {    return Math::sqrt(....);  }};


This has the added benefit of end-user class remaining the same syntax-wise, so there's no need to change the existing code.
Well, the_edd, I was wondering if bignum could be a typedef, an alias of a primary type, hence removing the need of using a custom type, to select, for the same primary type, a different implementation( algorithm ).
Quote:Original post by johnstanp
Well, the_edd, I was wondering if bignum could be a typedef, an alias of a primary type, hence removing the need of using a custom type, to select, for the same primary type, a different implementation( algorithm ).


Ah I see. No, I intended you to think of it as a user defined class.

Quote:Original post by the_edd
Quote:Original post by johnstanp
Well, the_edd, I was wondering if bignum could be a typedef, an alias of a primary type, hence removing the need of using a custom type, to select, for the same primary type, a different implementation( algorithm ).


Ah I see. No, I intended you to think of it as a user defined class.


Okay.
I could wrapp the primary types in my own classes, but I would have to overload the usual operators for those primary types.
Well, I took the most painful path, that is to say, adding a template parameter to the definition of my Vector3 class and to all of the classes that have a vector3 member object...

template<typename Real,class MathLib>class Vector3{     public:            ...     private:             ...};...template<typename Real,class MathLib>Real Vector3<Real,MathLib>::length()const{     return MathLib::SQRT( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );}


Of course, MathLib must define static functions( for example SQRT ): I prefer doing it that way, to avoid storing an instance of MathLib, for every Vector3 instanciated...And ah, I had to overload "operator=" and "Vector3( const Vector3& )" to allow "type conversion" between two Vector3 using different "Math Libraries".

[edit] That's the solution given by lexs. So thank you lexs. And thank you the_edd for your disponibility.[/edit]

This topic is closed to new replies.

Advertisement