#include <iostream>
using std::cout;
using std::endl;
class Test {
char *p;
int len;
public:
Test(char *_p);
~Test();
void print();
};
Test::Test(char *_p)
{
len = strlen(_p);
p = new char[len + 1];
if (!p) {
cout << "new char error" << endl;
exit(1);
}
strcpy(p, _p);
cout << "new char[len + 1]: " << _p << endl;
}
Test::~Test()
{
cout << "delete[] p: " << p << endl;
delete[] p;
}
void Test::print()
{
cout << "print: " << p << endl;
}
int main()
{
Test o1("c++"), o2("java"), o3("php");
o1.print();
o2.print();
o3.print();
return 0;
}
/*
run:
new char[len + 1]: c++
new char[len + 1]: java
new char[len + 1]: php
print: c++
print: java
print: php
delete[] p: php
delete[] p: java
delete[] p: c++
*/