#include <cstdio>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <new>
#include <string>
class Node {
public:
Node();
~Node();
void SetName(const char *name);
const char *GetName() const;
protected:
std::string mName;
};
Node::Node() {
}
Node::~Node() {
mName.clear();
}
void Node::SetName(const char *name) {
mName.assign(name);
}
const char *Node::GetName() const {
return mName.c_str();
}
inline void *New(size_t n) {
void *p;
fprintf(stdout, "new(%u)\n", (unsigned int)n); fflush(stdout);
p = malloc(n);
if (!p) {
fprintf(stderr, "ERROR: Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
return p;
}
inline void Delete(void *p) {
fprintf(stdout, "delete(%p)\n", p); fflush(stdout);
if (!p)
return;
free(p);
}
void *operator new(size_t n) throw(std::bad_alloc) { return New(n); }
void *operator new(size_t n, const std::nothrow_t &) throw() { return New(n); }
void operator delete(void *p) throw() { Delete(p); }
void operator delete(void *p, const std::nothrow_t &) throw() { Delete(p); }
int main() {
Node *p;
p = new Node();
p->SetName("Test");
printf("%s\n", p->GetName());
delete p;
return EXIT_SUCCESS;
}