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(){ } };
为上周题目中的 Fruit和Apple 添加 构造函数与 析构函数, 并在构造函数与析构函数中打印控制台信息,观察构造和析枸调用过程。然后为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;}
阅读全文
0 0
- new_delete重载实现_boolan_第二门课_第二周_作业
- 虚指针的对齐_boolan_第二门课_第一周_作业
- 关于父类析构函数为什么要定义为虚函数_boolan_第二门课_第一周
- 关于转换构造函数与转换函数_boolan_第二门课_第一周
- 指针_引用_内存管理new_delete
- STL中的list和vector使用(一)_boolan(博览网)_第三门课_第二周_作业
- 括号匹配_boolan(博览网)_第三门课_第三周_作业
- 作业11: 类_运算符重载
- 程序设计入门—Java语言_第二周编程作业
- C语言作业_第二堂
- C语言作业_第二堂
- C语言作业_第二堂
- 【deeplearning.ai】第二门课:提升深层神经网络——正则化的编程作业
- 第一行代码_第二章
- OC大作业_通讯录的实现
- 第02章 CORE C++_控制语句_选择_循环_分支_跳出_函数_声明_形参_返回值_重载_递归
- STL中的set和multiset使用(一)_boolan(博览网)_第三门课_第一周_作业
- Fireworks焦祺_第二周
- Ubuntu 16.04最新源
- 利用递归调用实现十进制转化成任意进制
- JMS学习笔记——ActiveMQ5
- ABP官方文档翻译 3.3 仓储
- gsoap编译C++文件
- new_delete重载实现_boolan_第二门课_第二周_作业
- USACO
- ABP官方文档翻译 3.2 值对象
- Topcoder 2016 TCO Algorithm Algo Semifinal 1 Hard
- ABP官方文档翻译 3.1 实体
- Servlet 文件下载实例
- CSS3 box-shadow 属性
- 欢迎使用CSDN-markdown编辑器
- C实现不定长数组