普通构造、拷贝构造、移动构造、析构的写法

class Str {
public:
	char *str;
	Str(char value[])
	{
		cout << "普通构造函数..." << endl;
		str = NULL;
		int len = strlen(value);
		str = (char *)malloc(len + 1);
		memset(str, 0, len + 1);
		strcpy_s(str, len + 1, value);
	}
	Str(const Str &s)
	{
		cout << "拷贝构造函数..." << endl;
		str = NULL;
		int len = strlen(s.str);
		str = (char *)malloc(len + 1);
		memset(str, 0, len + 1);
		strcpy_s(str, len + 1, s.str);
	}
	Str(Str &&s)
	{
		cout << "移动构造函数..." << endl;
		str = NULL;
		str = s.str;
		s.str = NULL;
	}
	~Str()
	{
		cout << "析构函数" << endl;
		if (str != NULL)
		{
			free(str);
			str = NULL;
		}
	}
};

猜你喜欢

转载自blog.csdn.net/venice0708/article/details/82222913