类和列表的动态创建和销毁,new、delete,clear

/***************************************************
  Construction、DeConstruction
  new、delete 、list、list.clear()
  version 1.0.8, May 10th, 2016

  Copyright (C) 2015-2016 Acheld CHEN

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Acheld CHEN       
  http://blog.csdn.net/acheld
  
 ****************************************************/
 
///////////////
//other cpp file call the Target Function
///////////////
void CXContainerTestDlg::OnBnClickedButton1()
{
	// TODO:

	extern XContainerMgr* theXContainerMgr;
	if (!theXContainerMgr){
		theXContainerMgr = new XContainerMgr();
	}
	theXContainerMgr->Run();
	
	DWORD oldTick = GetTickCount();
	while(TRUE){
		DWORD curTick = GetTickCount();
		DWORD differ = curTick - oldTick;
		if (differ > 1000){
			if (theXContainerMgr){
				delete theXContainerMgr;
				theXContainerMgr = NULL;
				return;
			}
		}
	}

}
//
/ ****************************************************/
//XContainerMgr.h

#pragma once
#include "XContainer.h"
class XContainerMgr
{
public:
	XContainerMgr(void);
	~XContainerMgr(void);
public:
	void Init();
	void Run();
protected:
	XContainer* m_pXContainer;

};

extern XContainerMgr* theXContainerMgr;


/////////////////////
//XContainerMgr.cpp
/////////////////////

#include "StdAfx.h"
#include "XContainerMgr.h"


XContainerMgr::XContainerMgr(void)
{
	m_pXContainer = NULL;
}


XContainerMgr::~XContainerMgr(void)
{
	if (m_pXContainer)
	{
		delete m_pXContainer;
		m_pXContainer = NULL;
	}
}

void XContainerMgr::Init()
{
	if (!m_pXContainer){
		m_pXContainer = new XContainer();
	}
}

void XContainerMgr::Run()
{
	Init();
}


/ ****************************************************/
//XContainer.h
#pragma once

#include "XContainerInterface.h"
#include "XContainerInstance.h"
#include "tagDef.h"

class XContainer
{
public:
	XContainer(void);
	~XContainer(void);
	void Init();
	void Run();
protected:
	XContainerInterface* m_pXContainerInterface;
	XContainerInstance* m_pXContainerInstance;
	strDataAsmList m_DataAsmlist;
};

/////////////////////
//XContainer.cpp
/////////////////////

#include "StdAfx.h"
#include "XContainer.h"


XContainer::XContainer(void)
{
	m_pXContainerInterface = NULL;
	m_pXContainerInstance = NULL;

	Init();
}


XContainer::~XContainer(void)
{
	if (m_pXContainerInterface){
		delete m_pXContainerInterface;
		m_pXContainerInterface = NULL;
	}

	if (m_pXContainerInstance){
		delete m_pXContainerInstance;
		m_pXContainerInstance = NULL;
	}

	strDataAsmListIter aiter = m_DataAsmlist.begin();
	for ( ; aiter != m_DataAsmlist.end(); aiter++){
		LPstrDataAsm pAsm = *aiter;
		if (pAsm){
			if (pAsm->name){
				delete [] pAsm->name;
				pAsm->name = NULL;
			}
			delete pAsm;
			pAsm = NULL;
		}
	}

	bool bempty = m_DataAsmlist.empty();
	if (!bempty){
		m_DataAsmlist.clear();
	}
}

void XContainer::Init()
{
	if (!m_pXContainerInterface){
		m_pXContainerInterface = new XContainerInterface();
	}
	if (!m_pXContainerInstance){
		m_pXContainerInstance = new XContainerInstance();
	}
	for (int m =0; m <3; m++){
		LPstrDataAsm pData = new strDataAsm();
		pData->id = m+1;
		strcpy(pData->name, "DataAsm");
		pData->score += 0.03*m;
		m_DataAsmlist.push_back(pData);
	}
	
}


void XContainer::Run()
{

}



/ ****************************************************/
//XContainerInterface.h
#pragma once
#include "XContainerInstance.h"
class XContainerInterface
{
public:
	XContainerInterface(void);
	~XContainerInterface(void);
	void Init();
	void Run();
protected:
	XContainerInstance* m_pXcontainerInstance;

};

/////////////////////
//XContainerInterface.cpp
/////////////////////

#include "StdAfx.h"
#include "XContainerInterface.h"


XContainerInterface::XContainerInterface(void)
{
	m_pXcontainerInstance =NULL;
	Init();
}


XContainerInterface::~XContainerInterface(void)
{
	if (m_pXcontainerInstance){
		delete m_pXcontainerInstance;
		m_pXcontainerInstance = NULL;
	}
}

void XContainerInterface::Init()
{
	if (!m_pXcontainerInstance){
		m_pXcontainerInstance = new XContainerInstance();
	}
}

void XContainerInterface::Run()
{

}


/ ****************************************************/
//XContainerInstance.h

#pragma once
#include "tagDef.h"
class XContainerInstance
{
public:
	XContainerInstance(void);
	~XContainerInstance(void);
	void Init();
	void Run();
protected:
	strDataAsmList m_DataAsmlist;
};

/////////////////////
//XContainerInstance.cpp
/////////////////////

#include "StdAfx.h"
#include "XContainerInstance.h"


XContainerInstance::XContainerInstance(void)
{
	Init();
}


XContainerInstance::~XContainerInstance(void)
{
	strDataAsmListIter aiter = m_DataAsmlist.begin();
	for ( ; aiter != m_DataAsmlist.end(); aiter++){
		LPstrDataAsm pAsm = *aiter;
		if (pAsm){
			if (pAsm->name){
				delete [] pAsm->name;
				pAsm->name = NULL;
			}
			delete pAsm;
			pAsm = NULL;
		}
	}

	bool bempty = m_DataAsmlist.empty();
	if (!bempty){
		m_DataAsmlist.clear();
	}
}

void XContainerInstance::Init()
{

	for (int m =0; m <3; m++){
		LPstrDataAsm pData = new strDataAsm();
		pData->id = m+1;
		strcpy(pData->name, "InstanceAsm");
		pData->score += 0.03*m;
		m_DataAsmlist.push_back(pData);
	}
}

void XContainerInstance::Run()
{

}


/ ****************************************************/
//tagDef.h

#pragma once

#include 
#include 
typedef struct tagstrDataAsm{
	int id;
	char* name;
	float score;
	tagstrDataAsm(){
		id = -1;
		name = new char[128];
		strcpy(name,"Asm");
		score = 0.98;
	}
}strDataAsm,*LPstrDataAsm;

typedef std::list strDataAsmList;
typedef strDataAsmList::iterator strDataAsmListIter;
///////////////////////


你可能感兴趣的:(类和列表的动态创建和销毁,new、delete,clear)