韩树民 发表于 2013-1-26 14:14:14

深入探索C++对象模型之对象

对象(一)
在C语言中,“数据”和“对数据的处理(函数)”分开声明的,也就是说,语言本身并没有支持“数据和函数”之间的关联性。例如,
   typedef struct point3d{      float x;      float y;      float z;   }point3d;
而在c++中,坐标类型和坐标数目都可以参数化
template<class type,int dim>class Point{public:Point();Point(type coords){   for(int index=0;index<dim;index++)       _coords=coords;    }type& operator[](int index){   assert(index>=0 && index < dim);   return _coords;}type operator[](int index) const{   assert(index>=0 && index < dim);   return _coords;}//------etc--------private:    type _coords;};inline   template<class type,int dim>ostream& operator<<(ostream &os,const Point<type,dim>& pt){   os<<"(";   for(int ix = 0;ix < dim-1;ix++)   os<<pt<<", ";os<<pt;os<<")";}
页: [1]
查看完整版本: 深入探索C++对象模型之对象