编写一个自定义的String类

一、简介

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

二、详解

1、代码实现:

(1)代码String.h:

#include <iostream>
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 <string.h>
#include "String.h"
#include <stdio.h>
//默认构造函数,用于创建空字符串
String::String()
{
	cout<<"---String constructor---"<<endl;
	len = 0;
	str = new char[1];
	str[0] = '\0';
}
//析构函数
String::~String()
{
	  //cout<<"---String destructor---"<<endl;
    delete []str;
    len=0;
}
//构造函数,带一个参数用来初始化字符串
String::String(const char*const cstr)
{
	/*len = strlen(cstr);
	str = new char[len+1];
	for (unsigned int i =0; i < len; i++) {
	  str[i] = cstr[i];
	}
	str[len]='\0';*/
	cout<<"---String constructor:char*---"<<endl;
	if (cstr == NULL) {
		len = 0;
    str = new char[1];
    memset(str, 0, len+1);
    if (str == NULL) return;
  }
  else {
  	 len = strlen(cstr);
     str = new char[len + 1];
     memset(str, 0, len+1);
     if (str == NULL) return;
     strncpy(str, cstr, len);
   }
}
//复制构造函数,默认是浅层复制,需重载
String::String(const String &rs)
{
	cout<<"---String copy constructor---"<<endl;
	len = rs.getlen();
  str = new char[len + 1];
  for(unsigned int i = 0 ;i < len; i++) {
    str[i] = rs.str[i];
  }
  str[len] = '\0';
  /*len = rs.getlen();
  str = new char[len+1];
  if (str == NULL) return;
  strcpy(str, rs.str);*/
}
//重载下标运算符[]
char&String::operator[](unsigned int length)
{
  if(length>len) return str[len-1];
  else return str[length];
}
//重载下标运算符[](const版本)
char String::operator[](unsigned int length)const
{
	//cout<<"String::operator[] const"<<endl;
  if(length>len) return str[len-1];
  else return str[length];
}
//重载复制运算符=,用于两个字符串之间的赋值
String &String::operator=(const String &rs)
{
	cout<<"String::operator="<<endl;
	if (this == &rs) return *this;
  delete []str;
  len = rs.getlen();
  str = new char[len + 1];
  for(int i = 0; i < len; i++) {
    str[i] = rs[i];     //重载下标运算符[]才可使用
    //str[i] = rs.str[i];
  }
  str[len] = '\0';
  return *this;
}
//重载加法运算符+
String String::operator+(const String &rs)
{
	/*char *st= new char[len + rs.getlen() + 1];
  memset(st, 0, len + rs.getlen() + 1);
  strcat(st, str);
  strcat(st, rs.str);
  return String(st);*/
  cout<<"String::operator+"<<endl; 
  unsigned int total = len + rs.getlen();
  char *tmpstr = new char[total + 1];
  unsigned int i, j;
  for (i = 0; i < len; i++) {
    tmpstr[i] = str[i];
  }
  for( j = 0; j < rs.getlen(); j++, i++) {
    tmpstr[i] = rs[j];
  }
  tmpstr[total] ='\0';
  String st(tmpstr);
  delete tmpstr;
  tmpstr = NULL;
  return st; 
}
//重载组合运算符+=
String String::operator+=(const String &rs)
{
  cout<<"String::operator+="<<endl; 
	int total = len + rs.getlen();
  //delete []str;
  char *tmpstr = new char[total + 1];   
  unsigned int i,j;
  for(i = 0; i < len; i++) {
    tmpstr[i] = str[i];
  }
  for(j = 0; j < rs.getlen(); j++,i++) {
    tmpstr[i] = rs[j];
  }
  tmpstr[total]='\0';
  delete []str;
  str = new char[total + 1];
  strncpy(str, tmpstr, total + 1);
  return *this;
  /*
  char *temp = str;  
  len = len + rs.getlen(); 
  str = new char[len + 1];  
  strcpy(str, temp);  
  strcat(_str, rs.getstr());  
  delete[] temp;  
  return *this;*/
}
//重载输出流运算符<<
ostream &operator<<(ostream &output, const String &str)
{
	output<<str.str;
  return output;
}
//重载输入流运算符>>
istream &operator>>(istream &input, String &str)
{
	input>>str.str;
  return input;
}
//重载小于运算符<
bool operator<(const String&str1,const String &str2)
{
	if(strcmp(str1.str, str2.str) < 0) return 1;
  else return 0;
}
//重载大于运算符>
bool operator>(const String&str1,const String &str2)
{
	if(strcmp(str1.str, str2.str) > 0) return 1;
  else return 0;
}
//重载等于运算符==
bool operator==(const String&str1,const String &str2)
{
	if(strcmp(str1.str, str2.str) == 0) return 1;
  else return 0;
}
//获取字符串长度
unsigned int String::getlen()const
{
	return len;
}
//获取字符串
const char*String::getstr()const
{
	return str;
}

(3)代码main.cpp:
#include <iostream>
#include <stdio.h>
#include "String.h"
using namespace std;

int main()
{
	String str1;                  //默认构造函数
	String str2(NULL);            //带参构造函数
	String str3("helloworld");    //带参构造函数
	cout<<"str3="<<str3.getstr()<<endl;
	
	String str4(str3);            //复制构造函数
	//String str4 = str3;         //复制构造函数
	cout<<"str4="<<str4.getstr()<<endl; 
	
	
	const String str5("tai");     //重载下标运算符[](const版本) 
	cout<<"str5[0]="<<str5[0]<<endl;
	
	String str6;
	str6 = str3;                  //重载复制运算符=
	cout<<"str6="<<str6.getstr()<<endl;

	String str7 = str3 + str5;    //重载加法运算符+
	cout<<"str7="<<str7.getstr()<<endl;
  
	String str8("123");
	str8 += str7;                 //重载组合运算符+=
	cout<<"str8="<<str8.getstr()<<endl;
	
	String str9;
	cin>>str9;                    //重载输入流运算符>>
	cout<<"str9="<<str9<<endl;    //重载输出流运算符<<
	
	String str10("compare String");
	String str11("compare string");
	int comp = str10 > str11;
	cout<< "str10大于str11:" << comp <<endl;    
	return 0;
}
(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系统中运行结果:)


三、总结

(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)本文是一些总结,也有无法理解的地方,不足之处还请指出,在此先感谢!

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,我可以为您提供一个简单的自定义类加载器的实现,可以加载指定位置的指定类到JVM中。请注意,由于安全性考虑,Java中的类加载器通常不允许加载系统类库或者核心类库,因此下面的示例只能加载自定义的类。 首先,我们需要创建一个继承自ClassLoader的自定义类加载器,重写findClass方法: ```java import java.io.*; public class CustomClassLoader extends ClassLoader { private String classPath; public CustomClassLoader(String classPath) { this.classPath = classPath; } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { try { byte[] classBytes = getClassBytes(name); if (classBytes == null) { throw new ClassNotFoundException(name); } else { return defineClass(name, classBytes, 0, classBytes.length); } } catch (IOException e) { throw new ClassNotFoundException(name); } } private byte[] getClassBytes(String name) throws IOException { String classFilePath = classPath + File.separatorChar + name.replace('.', File.separatorChar) + ".class"; InputStream in = new FileInputStream(classFilePath); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); in.close(); return out.toByteArray(); } } ``` 上面的代码中,我们定义了一个成员变量classPath,表示类文件所在的路径;重写了findClass方法,根据类名查找类文件并读取其内容,返回一个字节数组,然后使用defineClass方法将字节数组转换成Class对象。 然后,我们可以使用上面的自定义类加载器加载指定位置的指定类: ```java public class Test { public static void main(String[] args) throws Exception { String classPath = "/path/to/class/files"; String className = "com.example.MyClass"; CustomClassLoader loader = new CustomClassLoader(classPath); Class<?> clazz = loader.loadClass(className); Object obj = clazz.newInstance(); // do something with obj } } ``` 在上面的代码中,我们首先指定了类文件所在的路径和类名,然后创建一个自定义类加载器,并使用它加载指定的类。最后,我们可以使用newInstance方法创建一个对象,并进行一些操作。 需要注意的是,如果我们要在Java虚拟机中加载自定义的类,必须使用自定义的类加载器。否则,JVM将无法识别我们的类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌托邦2号

博文不易,支持的请给予小小打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值