copy.cc

来自「资深C++讲师授课代码」· CC 代码 · 共 38 行

CC
38
字号
#include <iostream>
#include <cstring>
using namespace std;

class String{
	char* str;
public:
	String(const char* s=""){
		str = new char[strlen(s)+1];
		strcpy(str, s);
		cout << "new " << (void*)str << ':' << str << endl;
	}
	~String(){
		cout << "delete " << (void*)str << ':' << str << endl;
		delete[] str; str=NULL;
	}
	String& operator=(const String& s2){
		if(this==&s2) return *this;
		cout << "delete " << (void*)str << ':' << str << endl;
		delete[] str;
		str = new char[strlen(s2.str)+1];
		strcpy(str, s2.str);
		cout << "new " << (void*)str << ':' << str << endl;
		return *this;
	}
	String(const String& s){
		str = new char[strlen(s.str)+1];
		strcpy(str, s.str);
		cout << "new " << (void*)str << ':' << str << endl;
	}
};
int main()
{
	String s1("good"), s2("morning");
	String s3(s1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?