首页 > 代码库 > 类的内部创建内部释放

类的内部创建内部释放

       C++在使用对象指针时经常要考虑这样一样问题:指针的初始化,对象何时创建,何时释放。这无疑给程序员加重了负担,程序员不仅要考虑业务逻辑,还要时时刻刻刻指针在何时创建和释放。那有没有什么办法可以解决这个问题呢?答案是肯定的,下面就通过一个例子来讲解这种模式。
       假设现在要仿真模拟一艘航空母舰。广义的航空母舰是指“航空母舰战斗群”,假设一支航空母舰战斗群(AVSG)由1艘航母(AircraftCarrier),1艘补给舰 (DepotShip),2艘防空导弹驱逐舰(Destroyer),4艘多功能护卫舰(Frigate),2艘093核潜艇(NuclearSubmarine)组成,若干舰载机(ShipboardAircraft)和预警机组成。现在用简单的类来模拟这些战机。
      假设有这样一个航空母舰类和一个舰载机类如下:

AircraftCarrier

/* 航空母舰
 * File:   AircraftCarrier.h
 * Author: luowf
 *
 * Created on July 31, 2014, 8:03 PM
 */

#ifndef AIRCRAFTCARRIER_H
#define	AIRCRAFTCARRIER_H

#include "stdafx.h"


class AircraftCarrier {
protected:
    AircraftCarrier();
    AircraftCarrier(const AircraftCarrier& orig);
    virtual ~AircraftCarrier();
    
public:
    void SetName(const string& name)
    {
        m_name = name;
    }
    
    string GetName()
    {
        return m_name;
    }
    
private:
    string m_name;
    
    friend class CVSG;
};

#endif	/* AIRCRAFTCARRIER_H */

ShipboardAircraft

/* 舰载机
 * File:   ShipboardAircraft.h
 * Author: luowf
 *
 * Created on August 1, 2014, 9:26 AM
 */

#ifndef SHIPBOARDAIRCRAFT_H
#define	SHIPBOARDAIRCRAFT_H

#include "stdafx.h"

class ShipboardAircraft {
protected:
    ShipboardAircraft();
    ShipboardAircraft(const ShipboardAircraft& orig);
    virtual ~ShipboardAircraft();
    
public:
    void SetName(const string& name)
    {
        m_name = name;
    }
    
    string GetName()
    {
        return m_name;
    }
    
private:
    string m_name;
    
    friend class CVSG;
};

#endif	/* SHIPBOARDAIRCRAFT_H */


      航空母舰战斗群(AVSG)有一艘航母(AircraftCarrier)和n艘舰载机(ShipboardAircraft),那么我们就可以把航母和舰载机的创建与销毁交由航空母舰战斗群负责。AVSG的类如下:

CVSG.h

/* 航空母舰战斗群
 * 
 * File:   CVSG.h
 * Author: luoweifu
 *
 * Created on July 31, 2014, 7:49 PM
 */

#ifndef CVSG_H
#define	CVSG_H

#include "stdafx.h"
#include <string>

using namespace std;

class AircraftCarrier;
class ShipboardAircraft;

class CVSG {
protected:
    CVSG();
    CVSG(const CVSG& orig);
    virtual ~CVSG();
public:
    //创建航母
    AircraftCarrier* CreateAircraftCarrier();
    //添加舰载机
    ShipboardAircraft* AddShipboardAircraft();
    void SetName(const string& name);
    string GetName();
private:
    typedef vector<ShipboardAircraft*>  ShipAircVec;
    ShipAircVec            m_vecShipboardAircraft;
    AircraftCarrier*      m_pAircraftCarrier;
    string                m_name;
};

#endif	/* CVSG_H */


CVSG.cpp

/* 航空母舰战斗群
 * 
 * File:   CVSG.cpp
 * Author: luoweifu
 * 
 * Created on July 31, 2014, 7:49 PM
 */

//==================================================

#include "CVSG.h"
#include "AircraftCarrier.h"
#include "ShipboardAircraft.h"

//==================================================

CVSG::CVSG() : m_pAircraftCarrier(NULL), m_name("") {
    m_vecShipboardAircraft.empty();
}

CVSG::CVSG(const CVSG& orig) {
}

CVSG::~CVSG() {
    if(m_pAircraftCarrier != NULL)
    {
        delete m_pAircraftCarrier;
        m_pAircraftCarrier = NULL;
    }
    for(ShipAircVec::iterator itr = m_vecShipboardAircraft.begin(); itr != m_vecShipboardAircraft.end(); itr ++)
    {
        if(*itr != NULL)
        {
            delete *itr;
            *itr = NULL;
        }
    }
    m_vecShipboardAircraft.clear();
}

AircraftCarrier* CVSG::CreateAircraftCarrier() {
    if(m_pAircraftCarrier == NULL)
    {
        m_pAircraftCarrier = new AircraftCarrier();
    }
    return m_pAircraftCarrier;
}

ShipboardAircraft* CVSG::AddShipboardAircraft() {
    ShipboardAircraft* pShipboardAircraft = new ShipboardAircraft();
    if(pShipboardAircraft != NULL)
    {
        m_vecShipboardAircraft.push_back(pShipboardAircraft);
    }
    return pShipboardAircraft;
}

void CVSG::SetName(const string& name)
{
    m_name = name;
}

string CVSG::GetName()
{
    return m_name;
}


      说明:AircraftCarrier和ShipboardAircraft两个类的构造函数和析构函数都定义成protected,且都定义AVSG为友元类,这样是从客观上保证了AircraftCarrier和ShipboardAircraft的对象不能在外面被创建和释放,只能由AVSG内部来创建和释放,而AircraftCarrier和ShipboardAircraft的调用者不用考虑对象何时创建和释放。