C++中浅拷贝

#include "pch.h"
#include <iostream>

#include<string.h>
using namespace std;
class MyString
{
public:
	char *buffer;
public:
	MyString(const char * init)
	{
		buffer = NULL;
		cout << "Default constructor: creating new MyString" << endl;
		if (init != NULL)
		{
			buffer = new char[strlen(init) + 1];
			strcpy(buffer, init);
		
			cout << "buffer points to: 0x" << hex;
			cout << (unsigned int*)buffer << endl;
		 }
	}


	~MyString()
	{
		 cout << "Invoking destructor, clearing up" << endl;
		 delete[] buffer;
	 }

	int GetLength()
	{
		return strlen(buffer);
	}
	const char *GetString()
	{
		return buffer;
	}


};

int main()
{
	MyString sayHello("Hello from String Class");
	MyString copysay = sayHello;
	cout << (unsigned int *)sayHello.buffer << endl;
	cout << (unsigned int *)copysay.buffer << endl;
	cout << (unsigned int )*copysay.buffer << endl;
	cout << *copysay.buffer << endl;
	return 0;
	
}

在这里插入图片描述
结果:
Default constructor: creating new MyString
buffer points to: 0x0000025F1DB4AC50
0000025F1DB4AC50
0000025F1DB4AC50
48
H
Invoking destructor, clearing up
Invoking destructor, clearing up

#include "pch.h"
#include <iostream>

#include<string.h>
using namespace std;
class MyString
{
public:
	char *buffer;
public:
	MyString(const char * init)
	{
		buffer = NULL;
		cout << "Default constructor: creating new MyString" << endl;
		if (init != NULL)
		{
			buffer = new char[strlen(init) + 1];
			strcpy(buffer, init);
		
			cout << "buffer points to: 0x" << hex;
			cout << (unsigned int*)buffer << endl;
		 }
	}


	~MyString()
	{
		 cout << "Invoking destructor, clearing up" << endl;
		 delete[] buffer;
	 }

	int GetLength()
	{
		return strlen(buffer);
	}
	const char *GetString()
	{
		return buffer;
	}


};
void UseMystr(MyString  str)
{
	cout<< "String buffer in MyString is " << str.GetLength();
	cout << " characters long" << endl;
	
	cout << "buffer contains: " << str.GetString() << endl;
	return;

}

int main()
{
	MyString sayHello("Hello from String Class");
	UseMystr(sayHello);

	return 0;
	
}
  • 新建对象Mystring str ,str中的buffer 和sayHello中的buffer 指向同一个内存空间,两次使用delete会出错。可以将UseMystr(MyString str)改为UseMystr(MyString & str)
#include "pch.h"
#include <iostream>

#include<string.h>
using namespace std;
class MyString
{
public:
	char *buffer;
public:
	MyString(const char * init)
	{
		buffer = NULL;
		cout << "Default constructor: creating new MyString" << endl;
		if (init != NULL)
		{
			buffer = new char[strlen(init) + 1];
			strcpy(buffer, init);
		
			cout << "buffer points to: 0x" << hex;
			cout << (unsigned int*)buffer << endl;
		 }
	}


	~MyString()
	{
		 cout << "Invoking destructor, clearing up" << endl;
		 delete[] buffer;
	 }

	int GetLength()
	{
		return strlen(buffer);
	}
	const char *GetString()
	{
		return buffer;
	}


};
void UseMystr(MyString  &str)
{
	cout<< "String buffer in MyString is " << str.GetLength();
	cout << " characters long" << endl;
	
	cout << "buffer contains: " << str.GetString() << endl;
	return;

}

int main()
{
	MyString sayHello("Hello from String Class");
	UseMystr(sayHello);
	cout << "test..." << endl;

	return 0;
	
}
  • 没有进行对象复制
    Default constructor: creating new MyString
    buffer points to: 0x0000021B0DBEAB30
    String buffer in MyString is 17 characters long
    buffer contains: Hello from String Class
    test…
    Invoking destructor, clearing up
#include "pch.h"
#include <iostream>

#include<string.h>
using namespace std;
class MyString
{
public:
	char *buffer;
public:
	MyString(const char * init)
	{
		buffer = NULL;
		cout << "Default constructor: creating new MyString" << endl;
		if (init != NULL)
		{
			buffer = new char[strlen(init) + 1];
			strcpy(buffer, init);
		
			cout << "buffer points to: 0x" << hex;
			cout << (unsigned int*)buffer << endl;
		 }
	}


	MyString(const MyString& copySource) // Copy constructor
		{
		buffer = NULL;
		cout << "Copy constructor: copying from MyString" << endl;
		if (copySource.buffer != NULL)
			{
			// allocate own buffer
			buffer = new char[strlen(copySource.buffer) + 1];
			
			// deep copy from the source into local buffer
				strcpy(buffer, copySource.buffer);
			
			cout << "buffer points to: 0x" << hex;
			cout << (unsigned int*)buffer << endl;
			}
		}


	~MyString()
	{
		 cout << "Invoking destructor, clearing up" << endl;
		 delete[] buffer;
	 }

	int GetLength()
	{
		return strlen(buffer);
	}
	const char *GetString()
	{
		return buffer;
	}


};
void UseMystr(MyString  str)
{
	cout<< "String buffer in MyString is " << str.GetLength();
	cout << " characters long" << endl;
	
	cout << "buffer contains: " << str.GetString() << endl;
	return;

}

int main()
{
	MyString sayHello("Hello from String Class");
	//MyString copysay = sayHello;
	MyString copysay(sayHello);
	cout << (unsigned int *)copysay.buffer << endl;
	cout << "test..." << endl;

	return 0;
	
}

Default constructor: creating new MyString
buffer points to: 0x00000275FA7BB0B0
Copy constructor: copying from MyString
buffer points to: 0x00000275FA7BAD50
00000275FA7BAD50
test…
Invoking destructor, clearing up
Invoking destructor, clearing up

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值