C++学习(String类的实现)

Big Three

在类的设计中,析构函数、拷贝构造函数、拷贝赋值函数合称为C++的三大函数,有时候忽略了可能没有什么影响,因为编译器会调用其默认的三大函数,而正好默认函数满足其需求,但是很多情况下还是需要自己定义。

什么时候需要显示定义拷贝构造函数:
一种最简单的判别方法,如果成员变量中有需要动态分配内存时,需要显式定义拷贝构造函数。
  成员变量中动态分配了内存,那么必定需要再析构函数中回收,因此我们需要显示定义析构函数回收内存空间。**指针变量在拷贝的过程中,默认的拷贝构造函数只会拷贝指针的地址,不会拷贝指针所指向的内容,**那么拷贝的对象与原对象的指针指向同一数据块,如果原对象的数据发生变化,那么拷贝的对象的数据也发生了变化,因为实际上他们是同一个东西。因此需要显式定义拷贝构造函数,要显式定义拷贝构造函数往往就需要显式定义拷贝构造函数来实现拷贝过程。

#pragma once
#include <iostream>
#include <string.h>
using namespace std;

#ifndef __MYSTRING__
#define __MYSTRING__

class String
{
public:
	String(const char* cstr = 0);
	//只要类带有指针,一定要写出下面的函数(Big Three)
	String(const String& str);//接受的是String自己这种东西,所以为拷贝构造
	String& operator = (const String& str);//右手边赋值的也是它自己这种东西,为拷贝赋值
	~String();//析构函数,当对象死亡的时候被调用

	char* get_c_str() const { return m_data; }//此时使用const因为成员函数并没有修改data的值

private:
	char* m_data;
};

//构造函数
inline
String::String(const char* cstr)
{
	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(const String& str)
{
	m_data = new char[strlen(str.m_data) + 1];
	strcpy(m_data, str.m_data);
}

//析构函数
inline 
String::~String()
{
	delete[] m_data;
}

//拷贝赋值函数
inline 
String& String::operator=(const String& str)
{
	if (this == &str)
		return *this;

	delete[] m_data;
	m_data = new char[strlen(str.m_data)];
	strcpy(m_data, str.m_data);
	return *this;
}

//重载<<
ostream& operator << (ostream& os, const String& str)
{
	os << str.get_c_str();
	return os;
}
#endif
#include "mystring.h"

int main()
{
	String s1();
	String s2("hello");

	String s3(s2);
	s3 = s2;
	cout << s3 << endl;
	s3 = s2;

	cout << s3 << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值