#include <iostream>
using std::cout;
using std::endl;
class Test {
char *p;
public:
Test(char *s);
Test(const Test &obj); // copy constructor
~Test() {
delete[] p;
}
char *get() { return p; }
};
Test::Test(char *s)
{
p = new char[strlen(s) + 1];
if (!p) {
cout << "Error: Allocation" << endl;
exit(1);
}
strcpy(p, s);
}
Test::Test(const Test &obj)
{
p = new char[strlen(obj.p) + 1];
if (!p) {
cout << "Error: Allocation" << endl;
exit(1);
}
strcpy(p, obj.p);
}
void print(Test obj)
{
char *s;
s = obj.get();
cout << s << endl;
}
int main()
{
Test o("c++");
print(o); // copy constructor run
return 0;
}
/*
run:
c++
*/