This seemed like a good solution. However, I'm having som type related
problems when using this approach. Are STL classes ment to be inherited
source does not support polymorphism. Wouldn't it be better to keep a
helge wrote:
What is the best way to implement a vector in space R3, i.e a vector
holding three floats, supporting arithmetic operations, dot and cross
product etc in c++? is there a standard library class for this?
Not a standard class, but if your requirements are not too high, you can
use the std::valarray<double>. It supports all the aritmetic operation,
it supports the sum of the elements so that the norm and the dot product
it's straightforward, and you'll have to define just the cross product.
you can also hide a little bit of information encapsulating the valarray
into a class:
class R3Vector
: public std::valarray<double>
{
R3Vector()
: std::valarray<double>(0.0, 3)
{
}
R3Vector(double a, double b, double c)
: std::valarray<double>(0.0, 3)
{
(*this)[0] = a;
(*this)[1] = b;
(*this)[2] = c;
}
R3Vector(const valarray<double>& v)
: std::valarray<double>(0.0, 3)
{
if(v.size() != 3)
error
else
copy
}
R3Vector crossProd(const R3Vector& v) const;
};
etc.
Regards,
Zeppe