li9.cpp

来自「一些关于软件质量保证与测试的资料」· C++ 代码 · 共 54 行

CPP
54
字号
// li9.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <stdio.h>
class MyString
{
public:
	char* mChars;
	MyString()
	{
		mChars=new char
			[strlen("default value")+1];
		strcpy(mChars,"default value");
	};
	~MyString()
	{
		if (mChars)
			delete []mChars;
		printf("destructor is calling\n");
	};
	MyString& operator= (const MyString& rhs);
};

MyString& MyString::operator = (const MyString& rhs)
{

	if(&rhs==this)
		return *this;

	if (rhs.mChars != NULL)
	{
		delete []mChars;
		mChars = new char [strlen(rhs.mChars)+1];
		strcpy(mChars, rhs.mChars);
	}
	else
	{
		mChars = NULL;
	}

	return *this;
}

int main(int argc, char* argv[])
{
	MyString a;
	printf("a.mChars is %s\n", a.mChars);
	a=a;
	printf("a.mChars is %s\n", a.mChars);
	return 0;
}

⌨️ 快捷键说明

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