构造函数与析构函数(四)---深拷贝与浅拷贝

深拷贝与浅拷贝

浅拷贝

String.h


#ifndef _STRING_H
#define _STRING_H
class String
{
public:
	String(const char* str = nullptr);
	~String();

	void Display();
private:
	char* str_;
};

#endif // _STRING_H


String.cpp

#include "String.h"
#include <string.h>
#include <iostream>
using namespace std;

String::String(const char* str)
{
	int len = strlen(str) + 1;
	str_ = new char[len];
	memset(str_, 0, sizeof(str_));
	strncpy_s(str_, sizeof(str_), str, sizeof(str));
}
String::~String()
{
	delete [] str_;
}

void String::Display()
{
	cout << str_ << endl;
}

main


#include "String.h"

int main()
{
	String s1("AAA");
	s1.Display();

	String s2 = s1;	//	调用默认的拷贝构造函数
	//	系统提供的默认拷贝构造函数实施的是浅拷贝s2.str_ = s1.str_
	s2.Display();

	return 0;
}

运行结果:
在这里插入图片描述

深拷贝

String.h

#ifndef _STRING_H
#define _STRING_H
class String
{
public:
	String(const char* str = nullptr);
	String(const String& other);	// xxxxxx

	~String();

	void Display();
private:
	char* str_;
};

#endif // _STRING_H


String.cpp

#include "String.h"
#include <string.h>
#include <iostream>
using namespace std;

String::String(const char* str)
{
	int len = strlen(str) + 1;
	str_ = new char[len];
	memset(str_, 0, sizeof(str_));
	strncpy_s(str_, sizeof(str_), str, sizeof(str));
}

String::String(const String& other)	// xxxxxx
{
	int len = strlen(other.str_) + 1;
	str_ = new char[len];
	memset(str_, 0, sizeof(str_));
	strncpy_s(str_, sizeof(str_), other.str_, sizeof(other.str_));
}


String::~String()
{
	delete [] str_;
}

void String::Display()
{
	cout << str_ << endl;
}

main

#include "String.h"

int main()
{
	String s1("AAA");
	s1.Display();

	String s2 = s1;	//	调用实现深拷贝的拷贝构造函数
	s2.Display();

	return 0;
}

运行结果:
在这里插入图片描述

赋值操作

深拷贝

String.h

#ifndef _STRING_H
#define _STRING_H
class String
{
public:
	String(const char* str = "");
	String(const String& other);

	~String();

	String& operator=(const String& other);	// xxxxxx

	void Display();
private:
	char* str_;
};

#endif // _STRING_H

String.cpp

#include "String.h"
#include <string.h>
#include <iostream>
using namespace std;

String::String(const char* str)
{
	int len = strlen(str) + 1;
	str_ = new char[len];
	memset(str_, 0, sizeof(str_));
	strncpy_s(str_, sizeof(str_), str, sizeof(str));
}

String::String(const String& other)
{
	int len = strlen(other.str_) + 1;
	str_ = new char[len];
	memset(str_, 0, sizeof(str_));
	strncpy_s(str_, sizeof(str_), other.str_, sizeof(other.str_));
}


String::~String()
{
	delete [] str_;
}

String& String::operator=(const String& other)	// xxxxxx
{
	if (this == &other)
		return *this;

	delete[] str_;

	int len = strlen(other.str_) + 1;
	str_ = new char[len];
	memset(str_, 0, sizeof(str_));
	strncpy_s(str_, sizeof(str_), other.str_, sizeof(other.str_));

	return *this;
}
void String::Display()
{
	cout << str_ << endl;
}

main

#include "String.h"

int main()
{
	String s1("AAA");
	s1.Display();

	String s2 = s1;	//	调用实现深拷贝的拷贝构造函数
	s2.Display();

	String s3;	// xxxxxx
	s3.Display();	// xxxxxx
	s3 = s2;	//	调用=运算符	// xxxxxx
	//	系统提供的默认=运算符实施的是浅拷贝s3.str_ = s2.str_
	s3.Display();	// xxxxxx

	return 0;
}

在这里插入图片描述

禁止拷贝

将拷贝构造函数、等号运算符设为private;并且不提供其实现。

空类默认产生的成员

class Empty
{
	Empty();	//	默认构造
	Empty(const Empty& );	//	默认拷贝构造
	~Empty();	//	默认析构
	Empty& operator=(const Empty& );	//	默认赋值运算符
	Empty* operator&();	//	取址运算符
	const Empty* operator&() const;	//	取址运算符const
};

1.默认构造
2.默认拷贝构造
3.默认析构
4.默认赋值运算符
5.取址运算符
6.取址运算符const
#include <iostream>
using namespace std;

class Empty
{
public:
	Empty* operator&()	//	取址运算符
	{
		return this;
	}
};

int main()
{
	Empty e;
	Empty *p = &e;

	cout << sizeof(Empty) << endl;	//	输出1
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值