Created
December 22, 2020 04:22
-
-
Save qichunren/029471b479e6b0860c12ba6d4f66dc34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> | |
#include <cstdlib> | |
#include <vector> | |
using namespace std; | |
class Str{ | |
public: | |
char *value; | |
Str(char s[]) | |
{ | |
cout<<"调用构造函数..."<<endl; | |
int len = strlen(s); | |
value = new char[len + 1]; | |
memset(value,0,len + 1); | |
strcpy(value,s); | |
} | |
Str(Str &v) | |
{ | |
cout<<"调用拷贝构造函数..."<<endl; | |
this->value = v.value; // Just demo | |
} | |
~Str() | |
{ | |
cout<<"调用析构函数...."<<endl; | |
if(value != NULL) | |
{ | |
printf("value:%s\n", value); | |
delete[] value; | |
value = NULL; | |
} | |
} | |
}; | |
int main() | |
{ | |
char s[] = "I love BIT"; | |
Str *a = new Str(s); | |
Str *b = new Str(*a); | |
delete a; | |
cout<<"b对象中的字符串为:"<<b->value<<endl; | |
delete b; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment