}
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;
}
}
什么感觉?...有一点喘不过来气了。
看起来已经到了极限了。但是这只是针对3*vector 或者vector + vector来说的。
想想如果计算a+3*(b+c),实际上是sum(vector,product(sum(vector,vector))).我们能不能将中间步骤
进一步简化呢?
可以的。不过需要进一步使用template.这个以后再看吧。可以去看blinn的文章。
最后抄一段blinn的话
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.
最后一句很精彩。但也许允许我有一点不同意见。class不但要便于使用,也要便于维护。除非真的有速度
的工程需要,否则我想我愿意牺牲速度来换取可读性的。但是cpp的可读性不一定是多重继承,接口,模板
也许更重要。我想考虑的是复杂的数据结构。比如一个从点的邻接矩阵找面,什么数据结构比较好,什么class
设计比较清晰?
那么回到vector的讨论上来,如果我想vector便于维护,至少可以从两个角度来想
第一,如果要将vector扩展到4d, 也就是说homogeneous coordinates,那么vector类的设计要怎么办?