首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

急求析构函数,该如何解决

2013-01-05 
急求析构函数#include iostream #include string using namespace stdclass myerror {std::string st

急求析构函数

#include <iostream>
 #include <string>
 using namespace std;
 
 class myerror
 {
     std::string str;
 public:
     myerror(const char* str):str(str){}
     const char* what()const
     {
         return str.c_str();
     }
 };
 class Base //纯虚基类
 {
 private:
     int x;
     int y;
 protected:
     enum {MAX_OBJ_NUM=3}; //能存入的最大对象数
 public:
     Base():x(0),y(0) {}
     Base(int x,int y):x(x),y(y) {}
     virtual void draw()=0; //纯虚函数
     void move();
     Base* Pd[MAX_OBJ_NUM];  //实际存放各派生类的对象地址
     virtual ~Base(){}
 };
 class Poit :virtual public Base //点
 {
 public:
     Poit():Base() {}
     Poit(int x,int y):Base(x,y) {}
     void draw();
     virtual ~Poit(){cout<<"poit\n";}  //这里应如何实现
 };
 
 class Line :virtual public Base //线
 {
 private:
     int a;
     int b;
 public:
     Line():Base(),a(0),b(0) {}
     Line(int x,int y,int a,int b):Base(x,y),a(a),b(b) {}
     void draw();
     virtual ~Line(){cout<<"line\n";}  //这里应如何实现
 };
 
 class Circle :virtual public Base //圆
 {
 private:
     int r;
 public:
     Circle():Base(),r(0) {}
     Circle(int x,int y,int r):Base(x,y),r(r) {}
     void draw();
     virtual ~Circle(){cout<<"circle\n";}  //这里应如何实现
 };
 class Graphics :public Poit,public Line, public Circle //图集
 {
 private:
     int i;
 public:
     Graphics():Base(),Poit(),Line(),Circle(),i(0) {}
     void task(Base *p);
     void draw();
     void fun(Base *b)
     {
         if(i>=(int)MAX_OBJ_NUM)
         {             
             throw myerror("-----FULL-----\n");
         }
         Pd[i++]=b;
     }
     virtual ~Graphics()  ////这里应如何实现
     {
         for(;i;)
         {


             cout<<i<<endl;
             delete Pd[--i];
         }
     }
 };
 void Base::move()
 {
     cout<<"将画笔移动到("<<x<<","<<y<<"),";
 }
 void Poit::draw()
 {
     Base::move();
     cout<<"画一个点"<<endl;
 }
 void Line::draw()
 {
     Base::move();
     cout<<"画一条到("<<a<<","<<b<<")的直线"<<endl;
 }
 void Circle::draw()
 {
     Base::move();
     cout<<"画一个半径为"<<r<<"的圆"<<endl;
 }
 void Graphics::draw()
 {
     for(int n=0; n<i; ++n)
     {
         cout<<"第"<<n+1<<"步:";
         Pd[n]->draw();
     }
 }
 void Graphics::task(Base *p)
 {
     fun(p);
 }
 int main()
 {
     Graphics g;
     cout<<"绘图步骤:"<<endl;
     try
     {
         g.task(new Poit(3,5));
         g.task(new Line(1,2,3,5));
         g.task(new Circle(3,4,5));
         g.task(new Line(8,6,21,15));
         g.task(new Poit(6,9));
         g.draw();
     }
     catch(myerror &err)
     {
         g.draw();
         cout<<err.what();
     }
     return 0;
 }
 


[解决办法]
虚函数的用法与目的你清楚吗?

你只需要在析构函数中对本类的资源进行析构就可以了.

虚析构函数作用跟 普通虚函数一样, 只是为了delete一个基类指针的时候能调用子类的析构函数保证资源都被释放.

class Poit 如果你这个类中没有申请任何其它资源,那么你什么也不用做.

Base* Pd[MAX_OBJ_NUM]只是你的Base类中有这个东西, 但Base类的析构函数中却没有对其做任何操作,不知道会不会有内存泄漏.
[解决办法]
给你改了改这个,其他的不知道从何处改好,
class Graphics :public Poit,public Line, public Circle //图集
{
public:
  Graphics():Base(),Poit(),Line(),Circle(),i(0) {}
  void task(Base *p);
  void draw();
  void fun(Base *b)
  {
    if(i>=(int)MAX_OBJ_NUM)
    {             
      throw myerror("-----FULL-----\n");
    }
    Pd[i++]=b;
  }
  virtual ~Graphics()  ////这里应如何实现


  {
    for(;i;)
    {
      cout<<i<<endl;
      delete Pd[--i];
    }
  }
protected:
  enum {MAX_OBJ_NUM=3}; //能存入的最大对象数
private:
  int i;
  Base* Pd[MAX_OBJ_NUM];  //实际存放各派生类的对象地址
};


[解决办法]
点、线、圆可以派生自同一父类,而Graphics是一个管理者,应该分开,不应该继承自一个父类,可以采用组合或者聚合的方式。

热点排行