编写一个自定义的String类

一、简介

       C++标准库中包含一个string类,提供了一套封装好的数据以及处理这些数据的函数。为了熟悉这个类的内存管理情况,实现一下自定义的String类,涉及构造函数、复制构造函数、析构函数、重载运算符的操作,主要关注其内部指针的内存的重分配。

二、详解

1、代码实现:

(1)代码String.h:

#include 
using namespace std;

//自定义String类,系统是string类
class String
{
	public:
		String();                              //默认构造函数,用于创建空字符串
		~String();                             //析构函数
		String(const char*const cstr);		     //构造函数,带一个参数用来初始化字符串
		String(const String &rs);              //复制构造函数,默认是浅层复制,需重载
		char& operator[](unsigned int length); //重载下标运算符[]
    char operator[](unsigned int length)const; //重载下标运算符[](const版本)  
		String &operator=(const String &rs);	 //重载复制运算符=,用于两个字符串之间的赋值
    String operator+(const String &rs);    //重载加法运算符+
    String operator+=(const String &rs);     //重载组合运算符+=
		friend ostream &operator<<(ostream &output, const String &str); //重载输出流运算符<<
		friend istream &operator>>(istream &input, String &str);        //重载输入流运算符>>
		friend bool operator<(const String&str1,const String &str2);    //重载小于运算符<
		friend bool operator>(const String&str1,const String &str2);    //重载大于运算符>
		friend bool operator==(const String&str1,const String &str2);   //重载等于运算符==

		unsigned int getlen()const;            //获取字符串长度
    const char*getstr()const;              //获取字符串
    
    //int operator++(){cout<<"++i\n";tmp++;return tmp;}    //int的重载自加运算符
    //int operator++(int){cout<<"i++\n";int tmp=rs;rs++;return tmp;}
	protected:
	private:
		unsigned int len;
    char *str;
};

(2)代码String.cpp:

#include 
#include "String.h"
#include 
//默认构造函数,用于创建空字符串
String::String()
{
	cout<<"---String constructor---"<len) return str[len-1];
  else return str[length];
}
//重载下标运算符[](const版本)
char String::operator[](unsigned int length)const
{
	//cout<<"String::operator[] const"<len) return str[len-1];
  else return str[length];
}
//重载复制运算符=,用于两个字符串之间的赋值
String &String::operator=(const String &rs)
{
	cout<<"String::operator="<(3)代码main.cpp:
#include 
#include 
#include "String.h"
using namespace std;

int main()
{
	String str1;                  //默认构造函数
	String str2(NULL);            //带参构造函数
	String str3("helloworld");    //带参构造函数
	cout<<"str3="<>str9;                    //重载输入流运算符>>
	cout<<"str9="< str11;
	cout<< "str10大于str11:" << comp <
(3)makefile:
CFLAGS = -g
DEFINED = #-D _VERSION
LIBS = 
CC = g++
INCLUDES = -I./
OBJS= main.o String.o
TARGET= main
all:$(TARGET)

$(TARGET):$(OBJS)
	$(CC) $(CFLAGS) -o $@ $(OBJS)

.SUFFIXES:.o .h
.SUFFIXES:.cpp .o
.cpp.o:
	$(CC) $(DEFINED) -c $(CFLAGS) -o $@ $<

ok:
	./$(TARGET)
clean:
	rm -f $(OBJS) $(TARGET) core *.log

2、运行结果:

(Centos6.3系统中运行结果:)

编写一个自定义的String类_第1张图片

三、总结

(1)重载"="和"+="运算符的函数中,返回类型必须与类名一致,主要为了处理str1=str2=str3接连赋值的情况。

(2)重载[ ]运算符的函数有2个,const类型针对const String str5("tai"),非const类型对应于String str5("tai")。

(3)C++不能重载的运算符有五个:"."(成员访问运算符)、" .*"(成员指针访问运算符)、"::"(域运算符)、"siezof"(长度运算符)、" ?:"(条件运算符),其他的运算符重载感兴趣的可以自己添加。

(4)在linxu编译器下:

String String::operator+(const String &rs)
{
  String st(tmpstr);
  return st;
}

测试String str1 = str2 + str3时,应该会调用复制构造函数,却没用调用,st地址却与str1地址相同,更离奇的是String &str1不能定义成引用,可能时编译器不同导致的。

(5)本文是一些总结,也有无法理解的地方,不足之处还请指出,在此先感谢!

你可能感兴趣的:(linuxC/C++编程)