new_delete重載實現_第二門課_第二週_作業

喜歡的朋友可以關注收藏一下:  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;
}


 

 

 

 

 



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章