Skip to content

Instantly share code, notes, and snippets.

@qichunren
Created December 22, 2020 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qichunren/029471b479e6b0860c12ba6d4f66dc34 to your computer and use it in GitHub Desktop.
Save qichunren/029471b479e6b0860c12ba6d4f66dc34 to your computer and use it in GitHub Desktop.
#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