When using object pointers, C ++ often needs to consider the following issues: pointer initialization, object creation, and release. This will undoubtedly increase the burden on programmers. programmers should not only consider the business logic, but also always learn when to create and release pointers. Is there any way to solve this problem? The answer is yes. Here is an example to illustrate this mode.
Suppose we want to simulate an aircraft carrier. Generally, an aircraft carrier refers to an aircraft carrier battle group. Assume that an aircraft carrier battle group (AVSG) is composed of one aircraft carrier (aircraftcarrier), one Supply ship (depotship), and two anti-aircraft missile destroyer (Destroyer ), four multi-functional frigate ships, two nuclearsubmarine ships, several shipboardaircraft ships and pre-warning machines. Now we use simple classes to simulate these fighters.
Assume that there is an aircraft carrier and a carrier:
Aircraftcarrier
/* Aircraft Carrier * file: aircraftcarrier. H * Author: luowf ** created on July 31,201 4, pm */# ifndef aircraftcarrier_h # defineaircraftcarrier_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-based aircraft * file: shipboardaircraft. H * Author: luowf ** created on August 1, 2014, 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 aircraftcarrier and shipboardaircraft. Then, we can assign the creation and destruction of aircraft carriers and aircraft carriers to the aircraft carrier battle group. AVSG classes are as follows:
Cvsg. h
/* Aircraft Carrier Battle Group ** file: cvsg. H * Author: luoweifu ** created on July 31,201 4, 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 (); Public: // create an aircraft carrier aircraftcarrier * createaircraftcarrier (); // Add 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
/* Aircraft Carrier Battle Group ** file: cvsg. CPP * Author: luoweifu ** created on July 31,201 4, 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) {response = new aircraftcarrier ();} return response;} shipboardaircraft * cvsg: addshipboardaircraft () {shipboardaircraft * response = new shipboardaircraft (); If (Response! = NULL) {response (pshipboardaircraft);} return pshipboardaircraft;} void cvsg: setname (const string & name) {m_name = Name;} string cvsg: getname () {return m_name ;}
Note:The constructor and destructor of the aircraftcarrier and shipboardaircraft classes are both defined as protected and AVSG is defined as the youyuan class, this ensures objectively that aircraftcarrier and shipboardaircraft objects cannot be created and released outside, but can only be created and released inside AVSG, the aircraftcarrier and shipboardaircraft callers do not need to consider when to create and release objects.