## CSDN博客

### Vector class (1)

Referrence:
Jim Blinn, "Optimizing C++ Vector Expression", in IEEE database
Kenneth I. Joy, "the Vector Data Type", http://graphics.cs.ucdavis.edu/CppNotes/homepage.html

PART I: on Constructor:
"All classes should have a copy constructor and an assignment operator."
That means, we should add two functions:
"Vector(const Vector&); and Vecotr& operator=(const Vector&);"
together with
"Vector(const double, const double, const double =0.0)"

and consider that
Vecotr& operator=(const Vector&)
the input and output of operator is the same.

PART II: on output and Serialize

PART III operator calling

Vector& operator+=(const Vector& v2)
{
_x+=v2._x; _y+=v2._y; _z+=v2._z;
return *this;
}
int operator== (const Vector& v1, const Vector &v2)
{
if((v1._x==v2._x)&&(v1._y==v2._y)&&(v1._z==v2._z))
return TRUE;
else
return FALSE;
}

Vector& operator+(const Vector& v1, const Vector &v2)
{
return Vector(A) +=B;
}
int operator!= (const Vector& v1, const Vector &v2)
{
return(!(v1==v2));
}

Part IV 随心所欲的计算

Part of the vast popularity of C++ is its class structure and operator overloading features
that allow the definition of mathematical operations for many class. We should know we can
make the C++ act as matlab!

Vector operator+ (const Vector& v1, const Vector& v2)
{
Vector vv;
vv._x = v1._x+v2._x;
vv._y = v1._y+v2._y;
vv._z = v1._z+v2._z;
return(vv);
}

double dot(const Vector& v1, const Vector& v2);
void Vector::normalize();
double Vector::length();

1.试试模板之类的东西。matlab可以不管int double 通吃的。
2.和矩阵的计算，接口
3.matlab 可以 d = [a,b,c]. C++好像只能d = vector(a,b,c)了。凑合着用了

Part V 编译错误

1. we should know the difference between const char *p (*p='c' is not allowed)
and char* const p (p++ is not allowed);
2. we should know the reason why Vector dot(const Vector&, const Vector &)
instead of Vector dot(Vector&,Vector &). Yes, one reason is it is more safe.
But another reason, if we define Vector dot(Vector&, Vector &), we will get
errors when using: a = dot(v1+v2,v3). The compiler will not allow you to change
(v1 + v2)!
3. do not forget to declare a const function: double length()const; will not
change the data members of the class.

Part VI 效率
C++运行比较慢就在于创建对象的开销。 一个奇怪的现象是C++大规模矩阵计算未必赶得上matlab.

class Sum{
const Vector &L;
const Vector &R;
public:
Sum(const Vector& Linit, const Vector& Rinit):L(Linit),R(Rinit){;}
float operator[](int i)const {return L[i]+R[i];}
}

void Vector::Evaluate(const Sum& e){
v[0]=e[0];v[1]=e[1];v[2]=e[2];
}
Sum operator+(const Vector& A,const Vector& B){
return Sum(A,B);
}

operator[]。比如Sum::operator[](int i)是{return L[i]+R[i]}

----yes, but.... do you know the cost of virtual function?

"如果类型T不影响类的行为T，你可以使用模板。如果T影响行为，你就需要虚函数，从而要使用继承。"

ok,let's go

//Sum<L,R>
template<class LT,class RT>
class Sum{
const LT &L;
const RT &R;
public:
Sum(const LT& Linit, const RT& Rinit):L(Linit),R(Rinit){;}
float operator[](int i)const{return L[i]+R[i];}
};

//Product<V>
template<class VT>
class Product{
float s;
const VT& V;
public:
Product(const float sinit,const VT& Vinit):s(sinit),V(Vinit){;}
float operator[](int i)const{return s*V[i];}
};

// class Vector
class Vector{
...
template <class T>
Vector(const T& e){
Evaluate(e);
}

template<class T>
void Evaluate(const T& e){
v[0]=e[0];v[1]=e[1];v[2]=e[2];
}

template<class T>
Vector& operator=(const T& e){
Evaluate(e);
return this;
}
}

Every programming language has a gimmick.
The gimmick of C++ is to put as much intelligence in the class library as possible,
to make things as easy as possible for the user of the those class. In spite of the fact
that there is a lot of complexity in the implementation of vector arithmetic, any user of
the Vector class does not see that complexity. They can create Vector class and perform
arithmetic on them with ease and with confidence. That is the classical trade-off in C++:
the need of the many out weigh the needs of  the few.

0 0