image.png

    code:
    image.png

    1. #ifndef OBJECT_H
    2. #define OBJECT_H
    3. namespace DJLib
    4. {
    5. class object
    6. {
    7. public:
    8. void* operator new (unsigned int size) throw();
    9. void operator delete (void* p);
    10. void* operator new[](unsigned int size) throw();
    11. void operator delete[](void* p);
    12. virtual ~object()= 0;
    13. };
    14. }
    15. #endif // OBJECT_H

    image.png

    1. #include "object.h"
    2. #include <cstdlib>
    3. #include <iostream>
    4. using namespace std;
    5. namespace DJLib
    6. {
    7. void* object::operator new (unsigned int size) throw()
    8. {
    9. cout<<"Object::operator new size = " << size << endl;
    10. return malloc(size);
    11. }
    12. void object::operator delete (void* p)
    13. {
    14. cout<<"Object::operator delete size "<< endl;
    15. free(p);
    16. }
    17. void* object::operator new[](unsigned int size) throw()
    18. {
    19. cout<<"Object::operator new[] size = " << size << endl;
    20. return malloc(size);
    21. }
    22. void object::operator delete[](void* p)
    23. {
    24. free(p);
    25. }
    26. object::~object()
    27. {
    28. }
    29. }

    image.png

    1. #ifndef EXCEPTION_H
    2. #define EXCEPTION_H
    3. #include "object.h"
    4. namespace DJLib
    5. {
    6. #define THROW_EXCEPTION(e,m) (throw e(m,__FILE__,__LINE__))
    7. class exception :public object//析构函数为纯虚函数,说明这个类为抽象类,抽象类不能生成对象
    8. {
    9. protected:
    10. char* m_message;//当前异常的具体信息
    11. char* m_location;//异常的具体位置
    12. void init(const char* message, const char* file, int line);
    13. public:
    14. exception(const char* message);
    15. exception(const char* file, int line);
    16. exception(const char* message,const char* file,int line);
    17. exception(const exception& e);
    18. exception& operator= (const exception& e);
    19. virtual const char* message() const;
    20. virtual const char* location() const;
    21. virtual ~exception() = 0; //(纯虚函数不需要提供实现,需等子类来完成),但析构函数需要实现
    22. };
    23. class arithmeticException : public exception//计算异常
    24. {
    25. public:
    26. arithmeticException():exception(0){}
    27. arithmeticException(const char* message) :exception(message){}
    28. arithmeticException(const char* file, int line):exception(file,line){}
    29. arithmeticException(const char* message,const char* file,int line):exception(message,file,line){}
    30. arithmeticException(const arithmeticException& e):exception(e){}
    31. arithmeticException& operator = (const arithmeticException& e)
    32. {
    33. exception::operator =(e);
    34. return *this;
    35. }
    36. };
    37. class nullPointerException : public exception//空指针异常 NullPointerException
    38. {
    39. public:
    40. nullPointerException():exception(0){}
    41. nullPointerException(const char* message) :exception(message){}
    42. nullPointerException(const char* file, int line):exception(file,line){}
    43. nullPointerException(const char* message,const char* file,int line):exception(message,file,line){}
    44. nullPointerException(const nullPointerException& e):exception(e){}
    45. nullPointerException& operator = (const nullPointerException& e)
    46. {
    47. exception::operator =(e);
    48. return *this;
    49. }
    50. };
    51. class indexOutOfBoundsException : public exception//越界异常 IndexOutOfBoundsException
    52. {
    53. public:
    54. indexOutOfBoundsException():exception(0){}
    55. indexOutOfBoundsException(const char* message) :exception(message){}
    56. indexOutOfBoundsException(const char* file, int line):exception(file,line){}
    57. indexOutOfBoundsException(const char* message,const char* file,int line):exception(message,file,line){}
    58. indexOutOfBoundsException(const indexOutOfBoundsException& e):exception(e){}
    59. indexOutOfBoundsException& operator = (const indexOutOfBoundsException& e)
    60. {
    61. exception::operator =(e);
    62. return *this;
    63. }
    64. };
    65. class noEnoughMemoryException : public exception//内存不足异常 NoEnoughMemoryException
    66. {
    67. public:
    68. noEnoughMemoryException():exception(0){}
    69. noEnoughMemoryException(const char* message) :exception(message){}
    70. noEnoughMemoryException(const char* file, int line):exception(file,line){}
    71. noEnoughMemoryException(const char* message,const char* file,int line):exception(message,file,line){}
    72. noEnoughMemoryException(const noEnoughMemoryException& e):exception(e){}
    73. noEnoughMemoryException& operator = (const noEnoughMemoryException& e)
    74. {
    75. exception::operator =(e);
    76. return *this;
    77. }
    78. };
    79. class invalldParameterException : public exception//参数错误异常 InvalldParameterException
    80. {
    81. public:
    82. invalldParameterException():exception(0){}
    83. invalldParameterException(const char* message) :exception(message){}
    84. invalldParameterException(const char* file, int line):exception(file,line){}
    85. invalldParameterException(const char* message,const char* file,int line):exception(message,file,line){}
    86. invalldParameterException(const invalldParameterException& e):exception(e){}
    87. invalldParameterException& operator = (const invalldParameterException& e)
    88. {
    89. exception::operator =(e);
    90. return *this;
    91. }
    92. };
    93. class invalldOperationException : public exception// 成员函数调用时,状态不正确抛异常invalldOperationException
    94. {
    95. public:
    96. invalldOperationException():exception(0){}
    97. invalldOperationException(const char* message) :exception(message){}
    98. invalldOperationException(const char* file, int line):exception(file,line){}
    99. invalldOperationException(const char* message,const char* file,int line):exception(message,file,line){}
    100. invalldOperationException(const invalldOperationException& e):exception(e){}
    101. invalldOperationException& operator = (const invalldOperationException& e)
    102. {
    103. exception::operator =(e);
    104. return *this;
    105. }
    106. };
    107. }
    108. #endif // EXCEPTION_H

    image.png

    1. #include "exception.h"
    2. #include <cstring>
    3. #include <stdlib.h>
    4. using namespace std;
    5. namespace DJLib {
    6. void exception::init(const char* message, const char* file, int line)
    7. {
    8. m_message = strdup(message);//拷贝信息到堆空间,m_message指向堆空间的信息
    9. if(file != NULL)
    10. {
    11. char sl[16] = {0}; //定义一个字符数组来接收转换的字符串
    12. itoa(line,sl,10);//发生异常的行号转换为字符
    13. m_location = static_cast<char *>(malloc(strlen(file)+strlen(sl)+2));//堆空间申请内存
    14. if(m_location !=NULL)
    15. {
    16. m_location = strcpy(m_location,file);
    17. m_location = strcat(m_location,":"); //拼接
    18. m_location = strcat(m_location,sl);
    19. }
    20. }
    21. else
    22. {
    23. m_location = NULL;
    24. }
    25. }
    26. exception::exception(const char* message)
    27. {
    28. init(message,NULL,0);
    29. }
    30. exception::exception(const char* file, int line)
    31. {
    32. init(NULL,file,line);
    33. }
    34. exception::exception(const char* message,const char* file,int line)
    35. {
    36. init(message,file,line);
    37. }
    38. exception::exception(const exception& e)//拷贝构造函数,深拷贝
    39. {
    40. m_message = strdup(e.m_message);
    41. m_location = strdup(e.m_location);
    42. }
    43. exception& exception::operator= (const exception& e)//赋值操作符
    44. {
    45. if(this != &e)
    46. {
    47. free(m_message);
    48. free(m_location);
    49. m_message = strdup(e.m_message);
    50. m_location = strdup(e.m_location);
    51. }
    52. return *this;
    53. }
    54. const char* exception::message() const
    55. {
    56. return m_message;
    57. }
    58. const char* exception::location() const
    59. {
    60. return m_location;
    61. }
    62. exception::~exception()
    63. {
    64. free(m_message);
    65. free(m_location);
    66. }
    67. }

    image.png

    1. #ifndef SMARTPOINTER_H
    2. #define SMARTPOINTER_H
    3. #include "object.h"
    4. namespace DJLib
    5. {
    6. template <typename T>
    7. class smartPointer :public object
    8. {
    9. protected:
    10. T* m_pointer;
    11. public:
    12. smartPointer(T* p = NULL)
    13. {
    14. m_pointer = p;
    15. }
    16. //拷贝构造函数与赋值操作符重载来实现一片堆空间只能由一个指针标识
    17. smartPointer(const smartPointer<T>& obj)//拷贝构造函数
    18. {
    19. m_pointer = obj.m_pointer;//当前对象的指针指向参数对象指向的堆空间
    20. const_cast<smartPointer<T>&>(obj).m_pointer = NULL;//参数对象指空(强制类型转换:去除const属性)
    21. }
    22. smartPointer<T>& operator = (const smartPointer<T>& obj)//赋值操作符重载
    23. {
    24. if(this != &obj)//不能自赋值
    25. {
    26. delete m_pointer;
    27. m_pointer = obj.m_pointer;
    28. const_cast<smartPointer<T>&>(obj).m_pointer = NULL;
    29. }
    30. return *this;//支持连续赋值
    31. }
    32. T* operator -> ()
    33. {
    34. return m_pointer;
    35. }
    36. T& operator * ()
    37. {
    38. return *m_pointer;
    39. }
    40. bool isNull()
    41. {
    42. return (m_pointer == NULL );
    43. }
    44. T* get()
    45. {
    46. return m_pointer;
    47. }
    48. ~smartPointer()
    49. {
    50. delete m_pointer;
    51. }
    52. };
    53. }
    54. #endif // SMARTPOINTER_H

    image.png

    1. #include <iostream>
    2. #include "object.h"
    3. #include "smartPointer.h"
    4. using namespace std;
    5. using namespace DJLib;
    6. int main()
    7. {
    8. smartPointer<int>* sp = new smartPointer<int>();
    9. delete(sp);
    10. return 0;
    11. }