引入计数类

// 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;
    }

}


此处略去main函数无数行


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值