64,631
社区成员
发帖
与我相关
我的任务
分享
class example
{
private:
char *s;
public:
example()
{
cout << "In example()" << endl;
}
example(const char *);
~example();
void show() {cout << s << endl;};
};
#include <iostream>
#include <cstring>
using namespace std;
class example
{
private:
char *s;
public:
example(const char *);
example(example const& ano){cerr<<"copy constructor called"<<endl;}
~example();
void show() {cout << s << endl;};
};
example::example(const char *p)
{
cerr<<"construct "<<this<<endl;
s = new char[strlen(p) + 1];
strcpy(s,p);
}
example::~example()
{
cerr<<"delete "<<this<<endl;
delete [] s;
}
int main(void)
{
example ex[4] = {example("hello"),example("hi"),example("yeah"),example("good")};
for (int i = 0; i < 4; i++)
ex[i].show();
return 0;
}
construct 0x7ffff1528880
construct 0x7ffff1528888
construct 0x7ffff1528890
construct 0x7ffff1528898
hello
hi
yeah
good
delete 0x7ffff1528898
delete 0x7ffff1528890
delete 0x7ffff1528888
delete 0x7ffff1528880