#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");
}