下面是一个具有深拷贝功能的C++字符串类:
1.CMystring.h
#pragma once
#include "stdafx.h"
class CMystring :
public CString
{
public:
CMystring(void);
CMystring::CMystring(const char* str);
~CMystring(void);
CMystring& CMystring::operator=(CMystring& rightValue);
};
2.CMystring.cpp
#include "Mystring.h"
CMystring::CMystring(void)
{
}
CMystring::CMystring(const char* str)
{
this->SetString(str,strlen(str));
}
CMystring::~CMystring(void)
{
}
CMystring& CMystring::operator=(CMystring& rightValue)
{
if(this==&rightValue) return *this;
if( this->GetLength()>0 )
{
this->ReleaseBuffer();
}
this->SetString(rightValue.GetBuffer(),rightValue.GetLength());
return *this;
}
此类是学习C++练习作品,仅供参考,如有不足,希望指出,再下感激不尽!切勿实际使用,我可不负责啊:-)
本文介绍了一个简单的C++字符串类实现,该类具备深拷贝功能,确保了对象间数据的独立性。通过构造函数及赋值操作符重载实现了内存的有效管理。
7283

被折叠的 条评论
为什么被折叠?



