
code:
#ifndef OBJECT_H#define OBJECT_Hnamespace DJLib{class object{public:void* operator new (unsigned int size) throw();void operator delete (void* p);void* operator new[](unsigned int size) throw();void operator delete[](void* p);virtual ~object()= 0;};}#endif // OBJECT_H

#include "object.h"#include <cstdlib>#include <iostream>using namespace std;namespace DJLib{void* object::operator new (unsigned int size) throw(){cout<<"Object::operator new size = " << size << endl;return malloc(size);}void object::operator delete (void* p){cout<<"Object::operator delete size "<< endl;free(p);}void* object::operator new[](unsigned int size) throw(){cout<<"Object::operator new[] size = " << size << endl;return malloc(size);}void object::operator delete[](void* p){free(p);}object::~object(){}}

#ifndef EXCEPTION_H#define EXCEPTION_H#include "object.h"namespace DJLib{#define THROW_EXCEPTION(e,m) (throw e(m,__FILE__,__LINE__))class exception :public object//析构函数为纯虚函数,说明这个类为抽象类,抽象类不能生成对象{protected:char* m_message;//当前异常的具体信息char* m_location;//异常的具体位置void init(const char* message, const char* file, int line);public:exception(const char* message);exception(const char* file, int line);exception(const char* message,const char* file,int line);exception(const exception& e);exception& operator= (const exception& e);virtual const char* message() const;virtual const char* location() const;virtual ~exception() = 0; //(纯虚函数不需要提供实现,需等子类来完成),但析构函数需要实现};class arithmeticException : public exception//计算异常{public:arithmeticException():exception(0){}arithmeticException(const char* message) :exception(message){}arithmeticException(const char* file, int line):exception(file,line){}arithmeticException(const char* message,const char* file,int line):exception(message,file,line){}arithmeticException(const arithmeticException& e):exception(e){}arithmeticException& operator = (const arithmeticException& e){exception::operator =(e);return *this;}};class nullPointerException : public exception//空指针异常 NullPointerException{public:nullPointerException():exception(0){}nullPointerException(const char* message) :exception(message){}nullPointerException(const char* file, int line):exception(file,line){}nullPointerException(const char* message,const char* file,int line):exception(message,file,line){}nullPointerException(const nullPointerException& e):exception(e){}nullPointerException& operator = (const nullPointerException& e){exception::operator =(e);return *this;}};class indexOutOfBoundsException : public exception//越界异常 IndexOutOfBoundsException{public:indexOutOfBoundsException():exception(0){}indexOutOfBoundsException(const char* message) :exception(message){}indexOutOfBoundsException(const char* file, int line):exception(file,line){}indexOutOfBoundsException(const char* message,const char* file,int line):exception(message,file,line){}indexOutOfBoundsException(const indexOutOfBoundsException& e):exception(e){}indexOutOfBoundsException& operator = (const indexOutOfBoundsException& e){exception::operator =(e);return *this;}};class noEnoughMemoryException : public exception//内存不足异常 NoEnoughMemoryException{public:noEnoughMemoryException():exception(0){}noEnoughMemoryException(const char* message) :exception(message){}noEnoughMemoryException(const char* file, int line):exception(file,line){}noEnoughMemoryException(const char* message,const char* file,int line):exception(message,file,line){}noEnoughMemoryException(const noEnoughMemoryException& e):exception(e){}noEnoughMemoryException& operator = (const noEnoughMemoryException& e){exception::operator =(e);return *this;}};class invalldParameterException : public exception//参数错误异常 InvalldParameterException{public:invalldParameterException():exception(0){}invalldParameterException(const char* message) :exception(message){}invalldParameterException(const char* file, int line):exception(file,line){}invalldParameterException(const char* message,const char* file,int line):exception(message,file,line){}invalldParameterException(const invalldParameterException& e):exception(e){}invalldParameterException& operator = (const invalldParameterException& e){exception::operator =(e);return *this;}};class invalldOperationException : public exception// 成员函数调用时,状态不正确抛异常invalldOperationException{public:invalldOperationException():exception(0){}invalldOperationException(const char* message) :exception(message){}invalldOperationException(const char* file, int line):exception(file,line){}invalldOperationException(const char* message,const char* file,int line):exception(message,file,line){}invalldOperationException(const invalldOperationException& e):exception(e){}invalldOperationException& operator = (const invalldOperationException& e){exception::operator =(e);return *this;}};}#endif // EXCEPTION_H

#include "exception.h"#include <cstring>#include <stdlib.h>using namespace std;namespace DJLib {void exception::init(const char* message, const char* file, int line){m_message = strdup(message);//拷贝信息到堆空间,m_message指向堆空间的信息if(file != NULL){char sl[16] = {0}; //定义一个字符数组来接收转换的字符串itoa(line,sl,10);//发生异常的行号转换为字符m_location = static_cast<char *>(malloc(strlen(file)+strlen(sl)+2));//堆空间申请内存if(m_location !=NULL){m_location = strcpy(m_location,file);m_location = strcat(m_location,":"); //拼接m_location = strcat(m_location,sl);}}else{m_location = NULL;}}exception::exception(const char* message){init(message,NULL,0);}exception::exception(const char* file, int line){init(NULL,file,line);}exception::exception(const char* message,const char* file,int line){init(message,file,line);}exception::exception(const exception& e)//拷贝构造函数,深拷贝{m_message = strdup(e.m_message);m_location = strdup(e.m_location);}exception& exception::operator= (const exception& e)//赋值操作符{if(this != &e){free(m_message);free(m_location);m_message = strdup(e.m_message);m_location = strdup(e.m_location);}return *this;}const char* exception::message() const{return m_message;}const char* exception::location() const{return m_location;}exception::~exception(){free(m_message);free(m_location);}}

#ifndef SMARTPOINTER_H#define SMARTPOINTER_H#include "object.h"namespace DJLib{template <typename T>class smartPointer :public object{protected:T* m_pointer;public:smartPointer(T* p = NULL){m_pointer = p;}//拷贝构造函数与赋值操作符重载来实现一片堆空间只能由一个指针标识smartPointer(const smartPointer<T>& obj)//拷贝构造函数{m_pointer = obj.m_pointer;//当前对象的指针指向参数对象指向的堆空间const_cast<smartPointer<T>&>(obj).m_pointer = NULL;//参数对象指空(强制类型转换:去除const属性)}smartPointer<T>& operator = (const smartPointer<T>& obj)//赋值操作符重载{if(this != &obj)//不能自赋值{delete m_pointer;m_pointer = obj.m_pointer;const_cast<smartPointer<T>&>(obj).m_pointer = NULL;}return *this;//支持连续赋值}T* operator -> (){return m_pointer;}T& operator * (){return *m_pointer;}bool isNull(){return (m_pointer == NULL );}T* get(){return m_pointer;}~smartPointer(){delete m_pointer;}};}#endif // SMARTPOINTER_H

#include <iostream>#include "object.h"#include "smartPointer.h"using namespace std;using namespace DJLib;int main(){smartPointer<int>* sp = new smartPointer<int>();delete(sp);return 0;}
