// MyCounter.h: interface for the CMyCounter class.
//
//
#if !defined(AFX_MYCOUNTER_H__24061F41_4DBF_486D_BC26_F77FDC519FC6__INCLUDED_)
#define AFX_MYCOUNTER_H__24061F41_4DBF_486D_BC26_F77FDC519FC6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "StdAfx.h"
class CMyCounter
{
private:
char *m_lpbuffer;
size_t m_nCount;
public:
void Init();
void Release();
size_t AddCounter();
size_t DecCounter();
const char* GetString() const;
public:
CMyCounter();
~CMyCounter();
CMyCounter(const char *);
};
#endif // !defined(AFX_MYCOUNTER_H__24061F41_4DBF_486D_BC26_F77FDC519FC6__INCLUDED_)
// MyCounter.cpp: implementation of the CMyCounter class.
//
//
#include "stdafx.h"
#include "MyCounter.h"
//
// Construction/Destruction
//
CMyCounter::CMyCounter()
{
Init();
}
CMyCounter::~CMyCounter()
{
Release();
}
CMyCounter::CMyCounter(const char *lpstr)
{
Init();
size_t nlen = strlen(lpstr) + sizeof(char);
m_lpbuffer = new char[nlen];
strcpy(m_lpbuffer, lpstr);
}
void CMyCounter::Init()
{
m_lpbuffer = NULL;
m_nCount = 1;
}
void CMyCounter::Release()
{
if(DecCounter() != 1)
{
DecCounter();
}
else
{
delete []this;
}
}
size_t CMyCounter::AddCounter()
{
return m_nCount++;
}
size_t CMyCounter::DecCounter()
{
return m_nCount--;
}
const char* CMyCounter::GetString() const
{
return m_lpbuffer;
}
// MyString.h: interface for the CMyString class.
//
//
#if !defined(AFX_MYSTRING_H__5BE039E4_D686_43CD_B864_CA6202377FF3__INCLUDED_)
#define AFX_MYSTRING_H__5BE039E4_D686_43CD_B864_CA6202377FF3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "MyCounter.h"
class CMyString
{
public:
CMyString();
~CMyString();
CMyString(const CMyString&);
CMyString(const CMyString*);
CMyString(const char*);
public:
const char* GetString() const;
size_t AddCounter();
size_t DecCounter();
private:
void Init();
void Release();
private:
CMyCounter *m_myCounter;
};
#endif // !defined(AFX_MYSTRING_H__5BE039E4_D686_43CD_B864_CA6202377FF3__INCLUDED_)
// MyString.cpp: implementation of the CMyString class.
//
//
#include "stdafx.h"
#include "MyString.h"
//
// Construction/Destruction
//
CMyString::CMyString()
{
Init();
}
CMyString::~CMyString()
{
Release();
}
CMyString::CMyString(const CMyString& obj)
{
m_myCounter = new CMyCounter(obj.m_myCounter->GetString());
m_myCounter = obj.m_myCounter;
m_myCounter->AddCounter();
}
CMyString::CMyString(const char* lpstr)
{
m_myCounter = new CMyCounter(lpstr);
m_myCounter->AddCounter();
}
CMyString::CMyString(const CMyString* obj)
{
m_myCounter = new CMyCounter(obj->m_myCounter->GetString());
m_myCounter = obj->m_myCounter;
m_myCounter->AddCounter();
}
const char* CMyString::GetString() const
{
return m_myCounter->GetString();
}
size_t CMyString::AddCounter()
{
return m_myCounter->AddCounter();
}
void CMyString::Init()
{
m_myCounter = NULL;
}
size_t CMyString::DecCounter()
{
return m_myCounter->DecCounter();
}
void CMyString::Release()
{
if(DecCounter() == 1)
{
delete m_myCounter;
m_myCounter = NULL;
}
else
{
m_myCounter->DecCounter();
m_myCounter = NULL;
}
//
//
#if !defined(AFX_MYCOUNTER_H__24061F41_4DBF_486D_BC26_F77FDC519FC6__INCLUDED_)
#define AFX_MYCOUNTER_H__24061F41_4DBF_486D_BC26_F77FDC519FC6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "StdAfx.h"
class CMyCounter
{
private:
char *m_lpbuffer;
size_t m_nCount;
public:
void Init();
void Release();
size_t AddCounter();
size_t DecCounter();
const char* GetString() const;
public:
CMyCounter();
~CMyCounter();
CMyCounter(const char *);
};
#endif // !defined(AFX_MYCOUNTER_H__24061F41_4DBF_486D_BC26_F77FDC519FC6__INCLUDED_)
// MyCounter.cpp: implementation of the CMyCounter class.
//
//
#include "stdafx.h"
#include "MyCounter.h"
//
// Construction/Destruction
//
CMyCounter::CMyCounter()
{
Init();
}
CMyCounter::~CMyCounter()
{
Release();
}
CMyCounter::CMyCounter(const char *lpstr)
{
Init();
size_t nlen = strlen(lpstr) + sizeof(char);
m_lpbuffer = new char[nlen];
strcpy(m_lpbuffer, lpstr);
}
void CMyCounter::Init()
{
m_lpbuffer = NULL;
m_nCount = 1;
}
void CMyCounter::Release()
{
if(DecCounter() != 1)
{
DecCounter();
}
else
{
delete []this;
}
}
size_t CMyCounter::AddCounter()
{
return m_nCount++;
}
size_t CMyCounter::DecCounter()
{
return m_nCount--;
}
const char* CMyCounter::GetString() const
{
return m_lpbuffer;
}
// MyString.h: interface for the CMyString class.
//
//
#if !defined(AFX_MYSTRING_H__5BE039E4_D686_43CD_B864_CA6202377FF3__INCLUDED_)
#define AFX_MYSTRING_H__5BE039E4_D686_43CD_B864_CA6202377FF3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "MyCounter.h"
class CMyString
{
public:
CMyString();
~CMyString();
CMyString(const CMyString&);
CMyString(const CMyString*);
CMyString(const char*);
public:
const char* GetString() const;
size_t AddCounter();
size_t DecCounter();
private:
void Init();
void Release();
private:
CMyCounter *m_myCounter;
};
#endif // !defined(AFX_MYSTRING_H__5BE039E4_D686_43CD_B864_CA6202377FF3__INCLUDED_)
// MyString.cpp: implementation of the CMyString class.
//
//
#include "stdafx.h"
#include "MyString.h"
//
// Construction/Destruction
//
CMyString::CMyString()
{
Init();
}
CMyString::~CMyString()
{
Release();
}
CMyString::CMyString(const CMyString& obj)
{
m_myCounter = new CMyCounter(obj.m_myCounter->GetString());
m_myCounter = obj.m_myCounter;
m_myCounter->AddCounter();
}
CMyString::CMyString(const char* lpstr)
{
m_myCounter = new CMyCounter(lpstr);
m_myCounter->AddCounter();
}
CMyString::CMyString(const CMyString* obj)
{
m_myCounter = new CMyCounter(obj->m_myCounter->GetString());
m_myCounter = obj->m_myCounter;
m_myCounter->AddCounter();
}
const char* CMyString::GetString() const
{
return m_myCounter->GetString();
}
size_t CMyString::AddCounter()
{
return m_myCounter->AddCounter();
}
void CMyString::Init()
{
m_myCounter = NULL;
}
size_t CMyString::DecCounter()
{
return m_myCounter->DecCounter();
}
void CMyString::Release()
{
if(DecCounter() == 1)
{
delete m_myCounter;
m_myCounter = NULL;
}
else
{
m_myCounter->DecCounter();
m_myCounter = NULL;
}
}
此处略去main函数无数行