c++之多态性动态数组的使用
2013-03-14 13:40
363 查看
读林锐博士的<高质量程序设计c/c++>关于数组指针的使用
注意点:
1.多态和指针算术运算不能混合使用,数组操作总会涉及到指针运算,因此多态和数组不应该混合运用
2.不要在数组中直接存放多态对象,而是用基类指针或者基类的智能指针代替。
附上代码:
注意点:
1.多态和指针算术运算不能混合使用,数组操作总会涉及到指针运算,因此多态和数组不应该混合运用
2.不要在数组中直接存放多态对象,而是用基类指针或者基类的智能指针代替。
附上代码:
#include <iostream> using namespace std; struct Point{ Point(int x,int y):m_x(x),m_y(y){} int m_x; int m_y; }; ostream& operator<<(ostream &os,const Point& p) { cerr<<"\n{\tHorizontalOrdinate="<<p.m_x\ <<"\n\tVerticalOrdinate ="<<p.m_y<<"\n}\n"; return os; } class Shape{ Point m_origin; public: Shape():m_origin(0,0){} explicit Shape(const Point& origin):m_origin(origin){} virtual ~Shape(){cerr<<"Shape::~Shape()"<<endl;} Point GetOrigin()const{return m_origin;} virtual void Draw()const{ cerr<<"Shape::Draw()"<<endl; cerr<<"Origin Point"<<m_origin<<endl; } }; class Circle: public Shape{ int m_radius; public: Circle():m_radius(1){} Circle(const Point&origin,int radius):Shape(origin),m_radius(radius){} virtual ~Circle(){cerr<<"Circle::~Circle()"<<endl;} int GetRadius()const{return m_radius;} virtual void Draw()const{ cerr<<"Circle::Draw()"<<endl; cerr<<"Origin Point:"<<GetOrigin()\ <<"Radius="<<m_radius<<endl<<endl; } }; class Rectangle:public Shape{ Point m_rightButtom;//right-bottom point,m_origin as left-top point public: Rectangle():m_rightButtom(1,1){} Rectangle(const Point& leftTop,const Point&rightBottom):Shape(leftTop),m_rightButtom(rightBottom){} virtual ~Rectangle(){ cerr<<"Rectangle::~Rectangle()"<<endl; } Point GetLeftTop()const{return GetOrigin();} Point GetRightBottom()const{return m_rightButtom;} virtual void Draw()const{ cerr<<"Rectangle::Draw()"<<endl; cerr<<"Left-top point:"<<GetLeftTop()\ <<"Right-bottom point:"<<m_rightButtom<<endl; } }; void DrawShapes( Shape *shapes[],int numOfShapes){ for(int i=0;i<numOfShapes;++i){ shapes[i]->Draw(); } } /* void DrawShapes(const Shape shapes[],int numOfShapes){ for (int i=0;i<numOfShapes;++i) { Shape *p =(Shape*)shapes; if(typeid(*p)==typeid(Shape)){ p->Draw(); p++; } else if (typeid(*p)==typeid(Circle)) { Circle*q =(Circle*)p; q->Draw(); p=++q; } else if (typeid(*p)==typeid(Rectangle)) { Rectangle*r =(Rectangle*)p; r->Draw(); p==++r; } else ; } } */ int main(){ Shape *p=new Shape(Point(1,1)); Circle*q = new Circle(Point(2,2),5); Rectangle *r=new Rectangle(Point(3,3),Point(4,4)); Shape *myShape[3]; myShape[0] = p; myShape[1] = q; myShape[2] = r; /* for (int i =0;i<3;i++) { myShape[i]->Draw(); } */ DrawShapes(myShape,3); }另外如果确实需要使用多态数组,可用STL配合普通指针和智能指针使用。
相关文章推荐
- 使用C++ vector类创建动态数组
- c++ primer plus 习题5.7(使用new为结构创建动态数组)
- C++中一维,二维动态数组的建立,与内存的释放delete 和 delete[] 的使用
- 五、C++中CArray动态数组的使用
- More Effective C++:不使用多态性数组
- C++学习笔记四之使用new来创建动态数组
- C++中动态数组CArray的使用
- C++动态数组使用方法
- 使用new来为动态分配结构数组并赋值
- 如何使用opencv的c++接口来读取、写结构体数组到xml文件中
- C++内存分配及变长数组的动态分配
- char*动态数组用c++ifstream和ofstream进行文件读写并用memset初始化
- C++动态数组--用动态数组求斐波那契的前N项
- C++动态申请数组和参数传递问题
- c++ 定义动态数组
- C++中的动态多维数组
- 动态数组使用
- C++中使用malloc(), free()定义初始化一维、二维、三维数组
- C++ 创建动态数组
- 7..使用反射动态创建数组和访问数组