C + + often takes into account the same problem: the initialization of pointers, when an object is created, and when it is released. This undoubtedly adds to the burden on the program ape, which not only considers the business logic, but also the moment when the pointer is created and released.
Is there any way to solve the problem? The answer is yes. Here is a sample to explain this pattern.
If you want to simulate an aircraft carrier today.
The
Generalized aircraft carrier refers to " aircraft carrier battle Group", if an aircraft carrier battle Group ( AVSG) is comprised of 1 aircraft carriers ( ). 1 Supply vessels (D ,2 anti-aircraft missile destroyer (destroyer). 4 Multipurpose frigate (F ), 2 093 nuclear submarines (N hipboardaircraft
These fighters are now modeled with simple classes.
If there is such a an aircraft carrier class and a carrier class for example the following:
Aircraftcarrier
/* Aircraft carrier * File: AircraftCarrier.h * AUTHOR:LUOWF * * Created on July, 8:03 PM */#ifndef Aircraftcarrier_h#de Fineaircraftcarrier_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
/* Carrier machine * File: ShipboardAircraft.h * AUTHOR:LUOWF * * Created on August 1, 9:26 AM */#ifndef Shipboardaircraft_ H#defineshipboardaircraft_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 */
The aircraft carrier Battle Group (AVSG) has an aircraft carrier (Aircraftcarrier) and N-Carrier aircraft (SHipboardaircraft). Then we will be able to build and destroy aircraft carrier and carrier aircraft to the aircraft carrier battle group responsible. AVSG classes such as the following:
CVSG.h
/* Aircraft carrier Battle Group * * File: CVSG.h * Author:luoweifu * * Created on July, 7:49 PM */#ifndef Cvsg_h#definecvsg_h #include "stdafx.h" #include <string>using namespace Std;class aircraftcarrier;class shipboardaircraft;class CVSG {protected: CVSG (); CVSG (const cvsg& orig); Virtual ~CVSG ();p ublic: //Create carrier aircraftcarrier* createaircraftcarrier (); Join the carrier Machine shipboardaircraft* Addshipboardaircraft (); void SetName (const string& name); String GetName ();p rivate: typedef vector<shipboardaircraft*> Shipaircvec; Shipaircvec M_vecshipboardaircraft; aircraftcarrier* M_paircraftcarrier; String m_name;}; #endif/* Cvsg_h */
CVSG.cpp
/* Aircraft carrier Battle Group * * File:CVSG.cpp * AUTHOR:LUOWEIFU * * Created on July, 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 Ai Rcraftcarrier (); } 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;}
Description: Aircraftcarrier andSconstructors and destructors for Hipboardaircraft two classes are defined as protected. And all define AVSG as friend class, this is objectively guaranteed aircraftcarrier andSHipboardaircraft objects cannot be created and freed outside. Can only be created and released internally by AVSG, while Aircraftcarrier andShipboardaircraft callers do not have to consider when objects are created and freed.
copyright notice: Article blog original article 。 Bloggers may use it without the consent of any commercial use. Reprint please indicate the source.
Internal class create a build