day_其他操作符的重载

day_其他操作符的重载

#include <iostream>
#include <stdlib.h> using namespace std; class A{
public:
A(void)
{
cout << "A::A(void)" << this << endl;
}
~A(void)
{
cout << "A::~A(void)" << this << endl;
} static void* operator new(size_t size)
{
void* p = malloc(size);
cout << "A::new(size_t) p=" << p << " size=" << size << endl;
return p;
}
static void operator delete (void* p)
{
cout << "A::delete(void*) p=" << p << endl;
free(p);
}
static void* operator new[](size_t size)
{
void* p = malloc(size);
cout << "A::new[](size_t) p= " << p << " size=" <<size << endl;
return p;
}
static void operator delete[](void* p)
{
cout << "A::delete[](size_t) p= " << p << endl;
free(p);
}
private:
int m_i; //
double m_d; //
char m_c[]; //
}; int main(void)
{
A* pa = new A;
cout << "main(void) pa = " << pa << endl;
delete pa;
cout << "----------------" << endl;
pa = new A[];
//size返回3*24+4个字节,其中4个字节放数组长度
cout << "main(void) pa = " << pa << endl;
cout << ((int*)pa)[-] << endl;
delete[] pa; return ;
}

day_其他操作符的重载

day_其他操作符的重载

day_其他操作符的重载

上一篇:httpd练习.md


下一篇:嵌入式开发板iTOP4412学习开发板