C++拷贝构造函数的深拷贝与浅拷贝知识点
2017-04-01 11:43
225 查看
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
using namespace std;
class MyString{
public:
char* pstr;
int length;
MyString(const char* str){
this->length = strlen(str) + 1;
this->pstr = new char[length]{0};
strcpy(this->pstr, str);
}
void show(){
cout << "lebgth " << length << " address " << (void*)pstr << " " << pstr << endl;
}
~MyString(){
delete[] pstr;
}
MyString(const MyString& mystr){
this->length = mystr.length;
this->pstr = new char[mystr.length]{0};
strcpy(this->pstr, mystr.pstr);
}
};
void main(){
MyString mystr("Nanjing");
mystr.show();
MyString mystr2(mystr);
mystr2.show();
mystr.~MyString();
mystr2.show();
cin.get();
}
#include <iostream>
using namespace std;
class MyString{
public:
char* pstr;
int length;
MyString(const char* str){
this->length = strlen(str) + 1;
this->pstr = new char[length]{0};
strcpy(this->pstr, str);
}
void show(){
cout << "lebgth " << length << " address " << (void*)pstr << " " << pstr << endl;
}
~MyString(){
delete[] pstr;
}
MyString(const MyString& mystr){
this->length = mystr.length;
this->pstr = new char[mystr.length]{0};
strcpy(this->pstr, mystr.pstr);
}
};
void main(){
MyString mystr("Nanjing");
mystr.show();
MyString mystr2(mystr);
mystr2.show();
mystr.~MyString();
mystr2.show();
cin.get();
}
相关文章推荐
- C++拷贝构造函数(深拷贝,浅拷贝)
- python知识点(三):复制(引用、拷贝、浅拷贝和深拷贝)
- c++拷贝构造函数、深拷贝、浅拷贝
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数详解,深拷贝,浅拷贝
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数和拷贝赋值运算符
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数(深拷贝与浅拷贝)
- C++拷贝构造函数(深拷贝,浅拷贝)
- C++拷贝构造函数(深拷贝,浅拷贝)
- JavaScript小知识点(一):深浅拷贝
- C++拷贝构造函数(深拷贝,浅拷贝)
- IO流操作实现文件拷贝\简单加密及相关知识点
- C++拷贝构造函数(深拷贝,浅拷贝)