http://blog.csdn.net/kennyrose/article/details/7486389
在C++中,对于一个空的类,编译器一般默认它会有4个成员函数:构造函数,析构函数,复制构造函数和赋值函数
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<iostream>
- using namespace std;
- class CMyString
- {
- friend std::ostream& operator<<( std::ostream& os, const CMyString& str);
- private:
- char* m_pData; // 私有变量保存字符串
- public:
- CMyString( const char* str = NULL ); // 构造函数
- CMyString( const CMyString& str ); // 拷贝构造函数
- ~CMyString( void ); // 析构函数
- CMyString& operator=( const CMyString& str ); // 赋值运算符
- CMyString operator+( const CMyString& str ); // 字符串连接
- bool operator==( const CMyString& str ); // 判断相等
- char operator[]( int idx ); // 数组索引
- int getLength(); // 返回长度
- };
- CMyString::CMyString( const char* str )
- {
- if ( !str )
- {
- this->m_pData = 0;
- }
- else
- {
- this->m_pData = new char[ strlen( str ) + 1 ];
- strcpy( this->m_pData, str );
- }
- }
- CMyString::CMyString( const CMyString& str )
- {
- if ( !str.m_pData )
- {
- this->m_pData = 0;
- }
- else
- {
- this->m_pData = new char[ strlen( str.m_pData ) + 1 ];
- strcpy( this->m_pData, str.m_pData );
- }
- }
- CMyString::~CMyString( void )
- {
- if ( this->m_pData)
- {
- delete[] this->m_pData;
- this->m_pData = 0;
- }
- }
- CMyString& CMyString::operator=( const CMyString& str)
- {
- if ( this != &str )
- {
- delete[] this->m_pData;
- if ( !str.m_pData )
- {
- this->m_pData = 0;
- }
- else
- {
- this->m_pData = new char[ strlen( str.m_pData ) + 1 ];
- strcpy( this->m_pData, str.m_pData );
- }
- }
- return *this;
- }
- CMyString CMyString::operator+( const CMyString& str )
- {
- CMyString newString;
- if ( !str.m_pData )
- {
- newString = *this;
- }
- else if ( !this->m_pData )
- {
- newString = str;
- }
- else
- {
- newString.m_pData = new char[ strlen( this->m_pData ) + strlen( str.m_pData ) + 1 ];
- strcpy( newString.m_pData, this->m_pData );
- strcat( newString.m_pData, str.m_pData );
- }
- return newString;
- }
- bool CMyString::operator==( const CMyString& str )
- {
- if ( strlen(this->m_pData) != strlen( str.m_pData ) )
- {
- return false;
- }
- else
- {
- return strcmp( this->m_pData, str.m_pData ) ? false : true;
- }
- }
- //实现了[]函数,调用[] return array element
- char CMyString::operator[]( int idx)
- {
- if ( idx > 0 && idx < strlen( this->m_pData ) )
- return this->m_pData[idx];
- }
- int CMyString::getLength()
- {
- return strlen(this->m_pData);
- }
- std::ostream& operator<<( std::ostream& os, const CMyString& str )
- {
- os<< str.m_pData;
- return os;
- }
- int main()
- {
- CMyString str1("i love ");
- CMyString str2("u baby");
- std::cout << "str1: " << str1 << std::endl;
- std::cout << "str2: " << str2 << std::endl;
- CMyString str3 = str1 + str2;
- std::cout << "str3: " << str3 << std::endl;
- CMyString str4 = str2;
- str1 = str2;
- std::cout << "str4: " << str4 << std::endl;
- std::cout << "str1: " << str1 << std::endl;
- std::cout << "str3 length: " << str3.getLength() << std::endl;
- std::cout << "str3[3]= " << str3[3] << std::endl;
- std::cout << ( str1 == str3 ) << std::endl;
- return 0;
- }
- // main output
- str1: i love
- str2: u baby
- str3: i love u baby
- str4: u baby
- str1: u baby
- str3 length: 13
- str3[3]= o
- 0
c++ string类实现