天天看点

C++拷贝构造函数与赋值函数

拷贝构造函数

拷贝构造函数是一种特殊的构造函数,具有单个形参,其是本类型的一个引用变量,且必须是const引用。

当创建一个类的对象时,拷贝构造函数被调用来对类的数据成员进行初始化和内存分配。在对象初始化的过程之中,拷贝构造函数提供了一个通过同类型的对象对它进行初始化的方式。

当我们没有显式地去声明定义一个类的拷贝构造函数的时候,C++编译器会默认生成对应的拷贝构造函数,这种默认的工作方式是浅拷贝,而如果类中用到需要动态分配内存的成员,就会出现内存安全的问题。这样我们就要自己去重载拷贝构造函数,实现深拷贝。

赋值函数

赋值函数是把一个新的对象赋值给原有的对象,如果原来的对象中分配有内存时,需要先把内存给释放掉。如果我们没有在类中显示重载对应类的赋值函数的话,C++编译器也会默认生成对应的赋值函数,这也是一种浅拷贝的形式。所以呢我们要手动重载赋值函数,实现深拷贝。

那什么时候会调用拷贝构造函数,什么时候会调用赋值函数呢?当对象不存在时,用其他的对象来对其初始化,这时调用的是拷贝构造函数;当对象已经存在,用其他对象来对它进行赋值操作时,调用的是赋值函数了。

看下面例子:

#include <iostream>
using namespace std;

class String
{
public:
	String(const char *str = NULL);
	String(const String &oth);
	String& operator=(const String &oth);
	void Visit();
	~String(void) {
		delete[] m_str;
	}
private:
	char *m_str;
};

void String::Visit() {
	cout << m_str << endl;
}
//构造函数
String::String(const char* str)
{
	if (NULL == str)
	{
		m_str = new char[1];
		*m_str = '\0';
	}
	else
	{
		m_str = new char[strlen(str) + 1];
		strcpy(m_str, str);
	}
}

//拷贝构造函数
String::String(const String &oth)
{
	cout << "调用了拷贝构造函数" << endl;
	m_str = new char[strlen(oth.m_str) + 1];
	strcpy(m_str, oth.m_str);
}

//赋值函数
String& String::operator=(const String &oth)
{
	cout << "调用了赋值函数" << endl;
	if (this == &oth)
		return *this;

	delete[] m_str; m_str = NULL;
	m_str = new char[strlen(oth.m_str) + 1];
	strcpy(m_str, oth.m_str);

	return *this;
}

int main(void) {
	String A("haha");
	String B = A;
	String C("xixi");
	C = A;
	return 0;
}
           

运行结果:

调用了拷贝构造函数
调用了赋值函数
           

继续阅读