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;
}