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; //4
    double m_d; //8
    char m_c[12]; //12
};

int main(void)
{
    A* pa = new A;
    cout << "main(void) pa = " << pa << endl;
    delete pa;
    cout << "----------------" << endl;
    pa = new A[3];
    //size返回3*24+4个字节,其中4个字节放数组长度
    cout << "main(void) pa = " << pa << endl;
    cout << ((int*)pa)[-1] << endl;
    delete[] pa;
    
    return 0;
}

 

posted @ 2019-04-07 20:31  鸿蒙过客  阅读(77)  评论(0编辑  收藏  举报