new_delete重载实现_boolan_第二门课_第二周_作业

来源:互联网 发布:江西淘宝村 编辑:程序博客网 时间:2024/05/22 03:13

喜欢的朋友可以关注收藏一下:  http://blog.csdn.NET/qq_31201973

本文如有错误,请及时私信我。

原版要求:

class Fruit{    int no;     double weight;     char key;  public:     void print() {   }     virtual void process(){   }  };       class Apple: public Fruit{     int size;     char type;  public:     void save() {   }     virtual void process(){   }  };  

 上周题目中的 FruitApple 添加 构造函数与 析构函数, 并在构造函数与析构函数中打印控制台信息,观察构造和析枸调用过程。然后为Apple类重载::operator new和 ::operator delete,在控制台打印信息,并观察调用结果。

先回忆一下new和delete


而new和delete是不可以重载的,只能重载operator new和operator delete

而要new和delete首先要有对应的构造函数和析构函数

Fruit() :no(0), weight(0), key('\0') { cout << "guo::Fruit::defaule ctor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl; }Fruit(int no_1, double weight_1, char key_1) :no(no_1), weight(weight_1), key(key_1){cout << "guo::Fruit::ctor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl;}~Fruit() { cout << "guo::Fruit::dtor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl; }


Apple() :size(0), type(0)  { cout << "guo::Apple::defaule ctor.this=" << this << "  size=" << size << "  type=" << type << endl; }Apple(int size_1, char type_1) :size(size_1), type(type_1){cout << "guo::Apple::ctor.this=" << this << "  size=" << size << "  type=" << type << endl;}~Apple() { cout << "guo::Apple::dtor.this=" << this << "  size=" << size << "  type=" << type << endl; }

子类构造函数会先调用父类的默认构造函数,然后在调用对应的构造函数

子类对象声明周期结束后会调用析构函数,而让他显示到dos窗口最简单的办法就是delete,调用delete时会先调用子类析构函数,然后是父类析构,最后才是operator delete.

然后我在类里声明静态重载

class Fruit{private:int no;double weight;char key;public:Fruit() :no(0), weight(0), key('\0') { cout << "guo::Fruit::defaule ctor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl; }Fruit(int no_1, double weight_1, char key_1) :no(no_1), weight(weight_1), key(key_1){cout << "guo::Fruit::ctor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl;}~Fruit() { cout << "guo::Fruit::dtor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl; }const int get_no() const { return no; }const double get_weight() const { return weight; }const char get_key() const { return key; }virtual void print() const { cout << "guo::Fruit::print.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl; }virtual void process(){   }};class Apple : public Fruit{private:int size;char type;public:Apple() :size(0), type(0)  { cout << "guo::Apple::defaule ctor.this=" << this << "  size=" << size << "  type=" << type << endl; }Apple(int size_1, char type_1) :size(size_1), type(type_1){cout << "guo::Apple::ctor.this=" << this << "  size=" << size << "  type=" << type << endl;}~Apple() { cout << "guo::Apple::dtor.this=" << this << "  size=" << size << "  type=" << type << endl; }static void* operator new(size_t size);static void operator delete(void* pdead, size_t size);static void* operator new[](size_t size);static void operator delete[](void* pdead, size_t size);void save() {   }const int get_size() const { return size; }const char get_type() const { return type; }virtual void print() const { cout << "guo::Apple::print.this=" << this << "  size=" << size << "  type=" << type << endl; }virtual void process() {   }};
operator new和operator new[] 除了名称之外函数内的自定义操作是一样的,operator delete和operator delete[]操作也是一样的
在这里我用了异常处理语句try 和 catch

inline void* Apple::operator new(size_t size){Apple* p;try{p = (Apple*)malloc(size);}catch(bad_alloc& e){cout << "内存不足无法调用Apple::void* operator new(size_t size)" << endl;delete p;return 0;}cout << "调用Apple::void* operator new(size_t size)," << "size=" << size << endl;return p;}inline void Apple::operator delete(void* pdead, size_t size){cout << "调用Apple::void operator delete(void* pdead, size_t size)," << "pdead=" << pdead << "  size=" << size << endl;free(pdead);}inline void* Apple::operator new[](size_t size){Apple* p;try{p = (Apple*)malloc(size);}catch (bad_alloc& e){cout << "内存不足无法调用Apple::void* operator new[](size_t size)" << endl;delete p;return 0;}cout << "调用Apple::void* operator new[](size_t size)," << "size=" << size << endl;return p;}inline void Apple::operator delete[](void* pdead, size_t size){cout << "调用Apple::void operator delete[](void* pdead, size_t size)," << "pdead=" << pdead << "  size=" << size << endl;free(pdead);}

全部代码:

/*fruit.h*//*    本程序在vs2013运行测试成功     */#ifndef __FRUIT_H__#define __FRUIT_H__namespace guo{#include<iostream>using namespace std;class Fruit{private:int no;double weight;char key;public:Fruit() :no(0), weight(0), key('\0') { cout << "guo::Fruit::defaule ctor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl; }Fruit(int no_1, double weight_1, char key_1) :no(no_1), weight(weight_1), key(key_1){cout << "guo::Fruit::ctor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl;}~Fruit() { cout << "guo::Fruit::dtor.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl; }const int get_no() const { return no; }const double get_weight() const { return weight; }const char get_key() const { return key; }virtual void print() const { cout << "guo::Fruit::print.this=" << this << "  no=" << no << "  weight=" << weight << "  key=" << key << endl; }virtual void process(){   }};class Apple : public Fruit{private:int size;char type;public:Apple() :size(0), type(0)  { cout << "guo::Apple::defaule ctor.this=" << this << "  size=" << size << "  type=" << type << endl; }Apple(int size_1, char type_1) :size(size_1), type(type_1){cout << "guo::Apple::ctor.this=" << this << "  size=" << size << "  type=" << type << endl;}~Apple() { cout << "guo::Apple::dtor.this=" << this << "  size=" << size << "  type=" << type << endl; }static void* operator new(size_t size);static void operator delete(void* pdead, size_t size);static void* operator new[](size_t size);static void operator delete[](void* pdead, size_t size);void save() {   }const int get_size() const { return size; }const char get_type() const { return type; }virtual void print() const { cout << "guo::Apple::print.this=" << this << "  size=" << size << "  type=" << type << endl; }virtual void process() {   }};inline void* Apple::operator new(size_t size){Apple* p;try{p = (Apple*)malloc(size);}catch(bad_alloc& e){cout << "内存不足无法调用Apple::void* operator new(size_t size)" << endl;delete p;return 0;}cout << "调用Apple::void* operator new(size_t size)," << "size=" << size << endl;return p;}inline void Apple::operator delete(void* pdead, size_t size){cout << "调用Apple::void operator delete(void* pdead, size_t size)," << "pdead=" << pdead << "  size=" << size << endl;free(pdead);}inline void* Apple::operator new[](size_t size){Apple* p;try{p = (Apple*)malloc(size);}catch (bad_alloc& e){cout << "内存不足无法调用Apple::void* operator new[](size_t size)" << endl;delete p;return 0;}cout << "调用Apple::void* operator new[](size_t size)," << "size=" << size << endl;return p;}inline void Apple::operator delete[](void* pdead, size_t size){cout << "调用Apple::void operator delete[](void* pdead, size_t size)," << "pdead=" << pdead << "  size=" << size << endl;free(pdead);}}#endif

/*fruit.h*//*    本程序在vs2013运行测试成功     */#include<iostream>#include"fruit.h"using namespace std;int main(){cout << "sizeof(Fruit)=" << sizeof(guo::Apple) << endl << endl;cout << "测试guo::Fruit::Fruit()构造函数:" << endl;guo::Fruit f1;cout << endl<<"测试guo::Fruit::Fruit(int no_1, double weight_1, char key_1)构造函数:" << endl;guo::Fruit f2{ 1, 1, 'a' };//{} 初始化不容易产生歧义cout << endl<<"测试guo::Apple::Apple()构造函数:" << endl;guo::Apple a1;cout << endl << "测试guo::Apple::Apple(int size_1, char type_1)()构造函数:" << endl;guo::Apple a2{ 5, 'a' };cout << endl << "测试guo::void* Apple::operator new(size_t size)重载函数:" << endl;guo::Apple* p1 = new guo::Apple{ 6, 'b' };cout << endl << "测试 全局 ::operator new 函数:" << endl;guo::Apple* px1 = ::new guo::Apple{ 6, 'b' };cout << endl << "测试guo::void Apple::operator delete(void* pdead, size_t size)重载函数 及 Fruit、Apple析构函数:" << endl;delete p1;cout << endl << "测试 全局  ::operator delete 函数 及 Fruit、Apple析构函数:" << endl;::delete px1;cout << endl << "测试guo::void* Apple::operator new[](size_t size)重载函数:" << endl;guo::Apple* p2 = new guo::Apple[5];cout << endl << "测试 全局 ::operator new[] 函数:" << endl;guo::Apple* px2 = ::new guo::Apple[5];cout << endl << "测试guo::void Apple::operator delete[](void* pdead, size_t size)重载函数 及 Fruit、Apple析构函数:" << endl;delete[] p2;cout << endl << "测试 全局  ::operator delete[] 函数 及 Fruit、Apple析构函数:" << endl;::delete[] px2;getchar();return 0;}