【手撕String类】并测试构造、析构函数调用过程

手撕string(为了方便测试加上了输出)
class String{
private:
	char* m_data;
public:
	String(const char* cstr);//默认构造
	String(const String& str);//拷贝构造
	String& operator=(const String& str);//拷贝赋值
	//为什么加&?因为operator=最后要返回的不是临时变量,是本来就存在的
	~String();//析构
	char* get_c_str()const{
		return m_data; 
	}
};
#include<cstring>//包含了strlen、strcpy函数(其实也应该手撕的)
//“建议”编译器将此函数变为内联函数,编译器自己会做决定
inline String::String(const char* cstr=0){
	cout<<"is default"<<endl;
	if(cstr){
		m_data=new char[strlen(cstr)+1];
		strcpy(m_data,cstr);
	}else{
		m_data=new char[1];
		*m_data='\0';
	}
}
inline String::~String(){
	cout<<"is delete"<<endl;
	delete[] m_data;
}
inline String::String(const String& str){
	cout<<"is copy constructor"<<endl;
	m_data=new char[strlen(str.m_data)+1];
	strcpy(m_data,str.m_data);
}
inline String& String::operator=(const String& str){
	cout<<"is ="<<endl;
	//考虑是不是自我赋值
	if(this==&str)return *this;
	delete[] m_data;
	m_data=new char[strlen(str.m_data)+1];
	strcpy(m_data,str.m_data);
	return *this;//可以传值也可以传引用
}
inline ostream& operator<<(ostream& os,const String& str){
	return os<<str.get_c_str()<<endl;
}
测试
int main(){
	String test="Hello";
	String a;
	String b=a;
	String c;
	c=test;
	{
		String d;
		String f(c);
		static String s="Hell!";
	}
	//cout<<s<<endl;//编译会报错,因为s的作用域已经在上一行结束
	cout<<c<<endl;
	return 0;
}
运行结果

在这里插入图片描述
可以看到s在出了{}范围后就无法再调用(已经出作用域了)但是并没有在作用域结束后就析构,而是在程序执行结束才析构。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

启立家的

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值