c++类的指针成员指向的内存的释放

来源:互联网 发布:前端程序员面试题 编辑:程序博客网 时间:2024/06/06 15:59

多态和指针成员指向的内存混在一起时,怎么确定内存的释放???

上代码

#include<iostream>

#include<string>
using namespace std;
class Person{
    public:
     string m_name;      //为了调试,访问属性破坏了封装性
     Person(){             //构造函数
     cout<<this<<" normal constructor of person class "<<endl;
     }  
     Person(string name){//重载构造函数
         cout<<this<<" normal constructor of person class "<<endl;
         m_name = name;
     }
     Person(const Person &mperson){//拷贝构造函数
         cout<<"copy constructor of Person"<<endl;
         m_name = mperson.m_name;
     }
     virtual ~Person(){ //为了使用多态调用子类的析构函数,释放内存
         cout<<this <<" Person destroy---"<<endl;
     }
     virtual void showName(){//成员函数,多态的使用
         cout<<" Decorated "<<m_name<<endl;
     }
};
//size(Person) = 1 + 4 -->8
class Finery : public Person{
     public:
     //为了调试,访问属性破坏了封装性
     //这只是一种聚合关系,不需要该类来负责构造和析构
     Person *m_component; //指针需要虚拟析构函数
     Finery( ){  //构造
         cout<<" normal constructor of finery "<<endl;
         m_component = NULL; //初始化指针
      }
     virtual ~Finery(){ //析构函数
            cout<<&m_component<<" Finery destroy---"<<endl;
            //这个指针不是在这里NEW的,也就不需要自己释放
 //聚合关系,没有负责关系 --即不负责析构和构造 --只是指向
             /*delete m_component;*/
                }
    void Decorate(Person *component) {
       // 指向他处释放原来内存
          if(m_component != NULL ) delete m_component ;
         //这里使用浅拷贝 还是 深拷贝? 那个合适?
          m_component = component;      
    }         
      virtual    void showName(){        //成员函数
               m_component->showName();
               }   
};
//size(Finery) = 5+4 ->12
class Tshirts : public Finery{
  public:
   Tshirts(){
        cout<<"constructor of Tshirts "<<endl;
   }
   virtual    ~Tshirts(){
       cout<<" Tshirts destroy "<<endl;
   }
   virtual void showName(){
          cout<<"Tshirt ";
          Finery::showName();
   }     
};
//size(Tshirt) = 5+4 - > 12
class BigTrouser:public Finery{
   public:
   BigTrouser(){
      cout<<"constructor of BigTrouser "<<endl;
   }
   virtual ~BigTrouser(){
       cout<<" bigtrouser destroy "<<endl;
   }
   virtual void showName(){
         cout<<"BigTrouser ";
         Finery::showName();
   }    
};
//size(BigTrouser) = 5+4 - > 12
//客户端
int main()
{   cout<<sizeof(Person)<<" "<<sizeof(Finery)<<" "<<sizeof(BigTrouser)<<" "<<sizeof(Tshirts)<<endl;
    cout<<"initial person"<<endl;
    //指针定义则初始化,否则置为NULL
    Person *p=new Person("小李");
    cout<<"initial BigTrouser"<<endl;
    BigTrouser *bt=new BigTrouser();
    cout<<"initial Tshirts"<<endl;
    Tshirts *ts=new Tshirts();
    cout<<"bt->Decorate(p)"<<endl;
    bt->Decorate(p);//初始化bt的person指针
    cout<<"ts->Decorate(bt)"<<endl;
    ts->Decorate(bt);//初始化ts指向的(多态)person指针
    //三个指针都指向了person
    cout<<"ts->show"<<endl;
    ts->showName();//show   
     cout<<" the adress of p  is "<<p<<endl;
     cout<<" the adress of bt->m_componet is "<<bt-> m_component<<endl;
     //下面这个会出现多态,先去调用bt->showName,然后调用父类的showName;
     cout<<" the adress of ts->m_component is "<<ts-> m_component<<endl;
    //这里还需要释放bt,p吗? 浅拷贝,不能多次使用delete
    //假如是深拷贝呢?
    cout<<"delete ts"<<endl;
    delete ts;    ts = NULL; //指针释放则置为NULL
    cout<<"delete bt"<<endl;
    delete bt; bt = NULL;
    cout<<"delete p"<<endl;
    delete p;     p = NULL;
    return 0;
}

/*  运行结果如下
initial person
0x6f17f8 normal constructor of person class
initial BigTrouser
0x6f1838 normal constructor of person class
 normal constructor of finery
constructor of BigTrouser
initial Tshirts
0x6f2578 normal constructor of person class
 normal constructor of finery
constructor of Tshirts
bt->Decorate(p)
ts->Decorate(bt)
ts->show
Tshirt BigTrouser  Decorated ??
 p = 0x6f17f8
 bt=  0x6f1838
 ts = 0x6f2578
 the adress of p  is 0x6f17f8
 the adress of bt->m_componet is 0x6f17f8
 the adress of ts->m_component is 0x6f1838
delete ts
 Tshirts destroy
0x6f2580 Finery destroy---
0x6f2578 Person destroy---
delete bt
 bigtrouser destroy
0x6f1840 Finery destroy---
0x6f1838 Person destroy---
delete p
0x6f17f8 Person destroy---
*/


原创粉丝点击