学了C++的重载机制之后,忍不住去探究一下C++中string类的内部实现过程。于是仿照这string类的功能,写了一个自己的string类练练手。可能还有些东西并未完善,后面再不断去完善。
参考代码如下所示:
Main.cpp
/*****************************************************
Copyright (C): 2017-2018
File name : Main.cpp
Author : Zhengqijun
Date : 2017年02月10日 星期五 09时56分07秒
Description :
Funcion List :
*****************************************************/
#include <iostream>
#include "String.h"
using namespace std;
int main()
{
String s1("hello");
s1.Display();
String s2(s1);
//String s2;
//s2.Display();
#if 0
String s3;
s3 = s1;
//s3 = "hello";
s3.Display();
//#endif
s1[1] = 'E';
s1.Display();
#endif
String s3("world");
#if 0
s3 += s1;
s3.Display();
cin >> s2;
cout << s2 << endl;
#endif
s3 = s1 + s2;
s3.Display();
String s4;
return 0;
}
String.h
/*****************************************************
Copyright (C): 2017-2018
File name : String.h
Author : Zhengqijun
Date : 2017年02月10日 星期五 09时48分13秒
Description :
Funcion List :
*****************************************************/
#ifndef _STRING_H_
#define _STRING_H_
#include <iostream>
using namespace std;
class String
{
public:
String();
String(char *str);
String(const String& other);
~String();
String& operator=(char *str);
String& operator=(const String& other);
char& operator[](unsigned int index);
const char& operator[](unsigned int index) const;
friend String operator+(const String& s1, const String& s2);
String& operator+=(const String& s);
friend ostream& operator<<(ostream& out, const String& s);
friend istream& operator>>(istream& in, String& s);
void Display();
private:
char *str_;
};
#endif
String.cpp
/*****************************************************
Copyright (C): 2017-2018
File name : String.cpp
Author : Zhengqijun
Date : 2017年02月10日 星期五 09时50分05秒
Description :
Funcion List :
*****************************************************/
#include <iostream>
#include <string.h>
#include "String.h"
using namespace std;
String::String()
{
str_ = new char['\0'];
cout << "default construct String!" << endl;
}
String::String(char *str)
{
cout << "construct String!" << endl;
int len = strlen(str) + 1;
str_ = new char[len];
memset(str_, 0, len);
strcpy(str_, str);
}
String::String(const String& other)
{
int len = strlen(other.str_) + 1;
str_ = new char[len];
memset(str_, 0, len);
strcpy(str_, other.str_);
}
String& String::operator=(const String& other)
{
if(this == &other)
{
return *this;
}
int len = strlen(other.str_) + 1;
delete [] str_;
str_ = new char[len];
memset(str_, 0, len);
strcpy(str_, other.str_);
return *this;
}
String& String::operator=(char *str)
{
delete [] str_;
int len = strlen(str) + 1;
str_ = new char[len];
memset(str_, 0, len);
strcpy(str_, str);
return *this;
}
char& String::operator[](unsigned int index)
{
//return str_[index];
return const_cast<char&>(static_cast<const String&>(*this)[index]);
}
const char& String::operator[](unsigned int index) const
{
return str_[index];
}
String operator+(const String& s1, const String& s2)
{
int len = strlen(s1.str_) + strlen(s2.str_) + 1;
char *newptr = new char[len];
memset(newptr, 0, len);
strcpy(newptr, s1.str_);
strcat(newptr, s2.str_);
return newptr;
}
String& String::operator+=(const String& s)
{
int len = strlen(s.str_) + strlen(str_) + 1;
char *newptr = new char[len];
memset(newptr, 0, len);
strcpy(newptr, str_);
strcat(newptr, s.str_);
String tmp(newptr);
delete [] str_;
str_ = new char[len];
strcpy(str_, newptr);
return *this;
}
ostream& operator<<(ostream& out, const String& s)
{
out << s.str_;
return out;
}
istream& operator>>(istream& in, String& s)
{
char buffer[4096];
in >> buffer;
int len = strlen(buffer)+1;
s.str_ = new char[len];
memset(s.str_, 0, len);
strcpy(s.str_, buffer);
return in;
}
String::~String()
{
delete [] str_;
cout << "destroy String!" << endl;
}
void String::Display()
{
cout << "str = " << str_ << endl;
}