STRING类的实现

C++面试题string的实现:

#include <iostream>
#include <cstring>
using namespace std;

class String{
private:
	char* m_str;
public:
	//1. 无参构造:
	String() :m_str(new char[1])
	{
		*m_str = '\0';
	}

	//2. 有参构造函数
	String(const char* str )
		: m_str(new char[strlen(str) + 1])
	{
		strcpy(m_str, str);
	}

	/*
	//缺省拷贝构造函数----浅拷贝
	String (string const& that):m_str(that.m_str){}
	*/

	//3. 自定义拷贝构造函数---深拷贝
	String(String const& that) 
		: m_str(new char[strlen(that.m_str) + 1])
	{
		strcpy(m_str, that.m_str);
	}

	//4. 拷贝赋值操作符---深拷贝
	String& operator= (String that) // yes, pass-by-value
	{
		if (&that != this){
			std::swap(m_str, that.m_str);
		}
		return *this;
	}

	//5. 析构函数
	~String(){
		if (m_str){
			delete[] m_str;
			m_str = NULL;
		}
	}

	size_t size() const
	{
		return strlen(m_str);
	}

	const char* c_str(void) const
	{
		return m_str;
	}
};

int main(){
	String str1("Hello,world!");
	cout << str1.c_str() << endl;

	String str2 = str1; //拷贝构造
	cout << str2.c_str() << endl;

	String str3("呵呵");
	str2 = str3;//赋值
	//	str2 operator= (str3);
	cout << str2.c_str() << endl;

	int a = 10, b = 20, c = 30;
	(a = b) = c;
	cout << a << endl; //30
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值