首页 > 其他分享 ><四>

<四>

时间:2023-01-05 14:56:33浏览次数:41  
标签: nullptr len char pname MyString new

#include <iostream>
#include <cstring>

class MyString {

public:

	MyString(const char * _pname=nullptr) {
		if (_pname == nullptr) {
		
			pname = new char[1];
			pname[0] = '\0';
		}
		else {
			int len = strlen(_pname);
			pname = new char[len + 1];
			strcpy(pname, _pname);
		}
		std::cout << "左值构造函数= " <<(int *) this << std::endl;
	}

	MyString(const MyString & _val) {
	
		int len = strlen(_val.pname);
		pname = new char[len + 1];
		strcpy(pname, _val.pname);
		std::cout << "左值拷贝构造="<< (int *) this <<" 原目标="<<(int *)(&_val) << std::endl;
	}


	MyString & operator=(MyString & _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = new char[strlen(_val.pname)+1];
		strcpy(pname, _val.pname);
		std::cout << "左赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}



	MyString( MyString && _val) {
		
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右值拷贝构造=" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
	}

	MyString & operator=(MyString && _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}



	~MyString() {

		if (pname != nullptr) {
			delete[] pname;
			pname = nullptr;

		}
		std::cout << "析构函数="<<(int *)this << std::endl;
	}

	const char * getChar() { return pname; }
private:

	char * pname;

};



MyString getString(MyString & ms) {

	const char * p = ms.getChar();
	int len = strlen(p);

	char * newp = new char[len + 1];

	MyString s(newp);

	return s;

}


int main(int argc, char *argv[])
{
	std::cout << "Hello world!" << std::endl;
	MyString s1("1234");
	MyString s2;
	s2 = getString(s1);
	system("pause");
}

#include <iostream>
#include <cstring>

class MyString {

public:

	MyString(const char * _pname=nullptr) {
		if (_pname == nullptr) {
		
			pname = new char[1];
			pname[0] = '\0';
		}
		else {
			int len = strlen(_pname);
			pname = new char[len + 1];
			strcpy(pname, _pname);
		}
		std::cout << "左值构造函数= " <<(int *) this << std::endl;
	}

	MyString(const MyString & _val) {
	
		int len = strlen(_val.pname);
		pname = new char[len + 1];
		strcpy(pname, _val.pname);
		std::cout << "左值拷贝构造="<< (int *) this <<" 原目标="<<(int *)(&_val) << std::endl;
	}


	MyString & operator=(MyString & _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = new char[strlen(_val.pname)+1];
		strcpy(pname, _val.pname);
		std::cout << "左赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}



	MyString( MyString && _val) {
		
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右值拷贝构造=" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
	}


	MyString & operator=(MyString && _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}



	~MyString() {

		if (pname != nullptr) {
			delete[] pname;
			pname = nullptr;

		}
		std::cout << "析构函数="<<(int *)this << std::endl;
	}

	const char * getChar() { return pname; }
private:

	char * pname;

};



MyString getString(MyString & ms) {

	const char * p = ms.getChar();
	int len = strlen(p);

	char * newp = new char[len + 1];

	MyString s(newp);

	return s;

}


int main(int argc, char *argv[])
{
	std::cout << "Hello world!" << std::endl;
	MyString s1("1234");
	MyString s2;
	s2 = getString(s1);
	system("pause");
}


#include <iostream>
#include <cstring>


class MyString {

public:

	MyString(const char * _pname=nullptr) {
		if (_pname == nullptr) {
		
			pname = new char[1];
			pname[0] = '\0';
		}
		else {
			int len = strlen(_pname);
			pname = new char[len + 1];
			strcpy(pname, _pname);
		}
		std::cout << "左值构造函数= " <<(int *) this << std::endl;
	}

	MyString(const MyString & _val) {
	
		int len = strlen(_val.pname);
		pname = new char[len + 1];
		strcpy(pname, _val.pname);
		std::cout << "左值拷贝构造="<< (int *) this <<" 原目标="<<(int *)(&_val) << std::endl;
	}


	MyString & operator=(MyString & _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = new char[strlen(_val.pname)+1];
		strcpy(pname, _val.pname);
		std::cout << "左赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}

	MyString & operator=(MyString && _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}



	~MyString() {

		if (pname != nullptr) {
			delete[] pname;
			pname = nullptr;

		}
		std::cout << "析构函数="<<(int *)this << std::endl;
	}

	const char * getChar() { return pname; }
private:

	char * pname;

};



MyString getString(MyString & ms) {

	const char * p = ms.getChar();
	int len = strlen(p);

	char * newp = new char[len + 1];

	MyString s(newp);

	return s;

	//return MyString(newp);
}


int main(int argc, char *argv[])
{
	std::cout << "Hello world!" << std::endl;
	MyString s1("1234");
	MyString s2;
	s2 = getString(s1);

	system("pause");
}

#include <iostream>
#include <cstring>

class MyString {

public:

	MyString(const char * _pname=nullptr) {
		if (_pname == nullptr) {
		
			pname = new char[1];
			pname[0] = '\0';
		}
		else {
			int len = strlen(_pname);
			pname = new char[len + 1];
			strcpy(pname, _pname);
		}
		std::cout << "左值构造函数= " <<(int *) this << std::endl;
	}

	MyString(const MyString & _val) {
	
		int len = strlen(_val.pname);
		pname = new char[len + 1];
		strcpy(pname, _val.pname);
		std::cout << "左值拷贝构造="<< (int *) this <<" 原目标="<<(int *)(&_val) << std::endl;
	}


	MyString & operator=(MyString & _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = new char[strlen(_val.pname)+1];
		strcpy(pname, _val.pname);
		std::cout << "左赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}

	MyString & operator=(MyString && _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}

	~MyString() {

		if (pname != nullptr) {
			delete[] pname;
			pname = nullptr;

		}
		std::cout << "析构函数="<<(int *)this << std::endl;
	}

	const char * getChar() { return pname; }
private:

	char * pname;

};

MyString getString(MyString & ms) {

	const char * p = ms.getChar();
	int len = strlen(p);
	char * newp = new char[len + 1];
	return MyString(newp);
}


int main(int argc, char *argv[])
{
	std::cout << "Hello world!" << std::endl;
	MyString s1("1234");
	MyString s2;
	s2 = getString(s1);

	system("pause");
}

上面没有左值拷贝构造函数

#include <iostream>
#include <cstring>


class MyString {

public:

	MyString(const char * _pname=nullptr) {
		if (_pname == nullptr) {
		
			pname = new char[1];
			pname[0] = '\0';
		}
		else {
			int len = strlen(_pname);
			pname = new char[len + 1];
			strcpy(pname, _pname);
		}
		std::cout << "左值构造函数= " <<(int *) this << std::endl;
	}

	MyString(const MyString & _val) {
	
		int len = strlen(_val.pname);
		pname = new char[len + 1];
		strcpy(pname, _val.pname);
		std::cout << "左值拷贝构造="<< (int *) this <<" 原目标="<<(int *)(&_val) << std::endl;
	}


	MyString & operator=(MyString & _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = new char[strlen(_val.pname)+1];
		strcpy(pname, _val.pname);
		std::cout << "左赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}


	MyString( MyString && _val) {
		
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右值拷贝构造=" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
	}

	MyString & operator=(MyString && _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}

	~MyString() {

		if (pname != nullptr) {
			delete[] pname;
			pname = nullptr;

		}
		std::cout << "析构函数="<<(int *)this << std::endl;
	}

	const char * getChar() { return pname; }
private:

	char * pname;

};

MyString getString(MyString & ms) {

	const char * p = ms.getChar();
	int len = strlen(p);

	char * newp = new char[len + 1];

	return MyString(newp);
}


int main(int argc, char *argv[])
{
	std::cout << "Hello world!" << std::endl;
	MyString s1("1234");
	MyString s2;
	s2 = getString(s1);
	system("pause");
}

#include <iostream>
#include <cstring>

class MyString {

public:

	MyString(const char * _pname=nullptr) {
		if (_pname == nullptr) {
		
			pname = new char[1];
			pname[0] = '\0';
		}
		else {
			int len = strlen(_pname);
			pname = new char[len + 1];
			strcpy(pname, _pname);
		}
		std::cout << "左值构造函数= " <<(int *) this << std::endl;
	}

	MyString(const MyString & _val) {
	
		int len = strlen(_val.pname);
		pname = new char[len + 1];
		strcpy(pname, _val.pname);
		std::cout << "左值拷贝构造="<< (int *) this <<" 原目标="<<(int *)(&_val) << std::endl;
	}


	MyString & operator=(MyString & _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = new char[strlen(_val.pname)+1];
		strcpy(pname, _val.pname);
		std::cout << "左赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}


	MyString( MyString && _val) {
		
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右值拷贝构造=" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
	}

	MyString & operator=(MyString && _val) {
	    
		delete[]pname;
		pname = nullptr;
		pname = _val.pname;
		_val.pname = nullptr;
		std::cout << "右赋值函数" << (int *) this << " 原目标=" << (int *)(&_val) << std::endl;
		return *this;
	}

	~MyString() {

		if (pname != nullptr) {
			delete[] pname;
			pname = nullptr;

		}
		std::cout << "析构函数="<<(int *)this << std::endl;
	}

	const char * getChar() { return pname; }
private:

	char * pname;

};

MyString getString(MyString & ms) {

	const char * p = ms.getChar();
	int len = strlen(p);

	char * newp = new char[len + 1];

	return MyString(newp);
}


int main(int argc, char *argv[])
{
	std::cout << "Hello world!" << std::endl;
	MyString s1("1234");

	MyString s3 = getString(s1);

	system("pause");
}

标签:,nullptr,len,char,pname,MyString,new
From: https://www.cnblogs.com/erichome/p/17027182.html

相关文章