#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
class Student
{
public:
Student(int id, const char *name)
: _id(id)
, _name(new char[strlen(name) + 1]())
{
cout << "Student(int, const char *)" << endl;
strcpy(_name, name);
}
static void *operator new(size_t sz)
{
cout << "void *operator new(size_t)" << endl;
void *pret = malloc(sz);
return pret;
}
static void operator delete(void *ptr)
{
cout << "void operator delete (void *)" << endl;
free(ptr);
}
void print() const
{
cout << "id: " << _id << endl
<< "name: " << _name << endl;
}
void destroy()
{
delete this;
}
private:
~Student()
{
cout << "~Student()" << endl;
if(_name)
{
delete [] _name;
_name = nullptr;
}
}
private:
int _id;
char *_name;
};
int main(int argc, char **argv)
{
Student *pstu = new Student(10, "xiaohong");
pstu->print();
pstu->destroy();
return 0;
}