顶层类+智能指针类+异常类的实现

 创建的数据结构库由顶层父类库,智能指针类库,异常类库,各种数据结构类库组成。

 所有类继承于顶层父类库,统一用new或则new[]关键字申请内存失败的情况。

 智能指针类库保证指针生命周期结束时自动释放指向的内存空间,一个空间只由一个指针指向,不可进行指针间的运算。

 异常类库定义了顶层异常类用于被其他异常所继承。 其他异常包括 计算异常,空指针异常,越界异常,内存不足异常,参数错误异常, 成员函数状态异常

1. 顶层父类库

TopClass.h

/*
*    顶层父类:SpuperClass,统一 new,new[]失败时为返回NULL,不抛出异常
*/

#ifndef TOPCLASS_H
#define TOPCLASS_H

namespace DSL
{
    class TopClass
    {
    public:
        void* operator new (size_t)throw();  // 重写new 返回值必须是void* 参数必须是size_t
        void  operator delete(void* p);    
        void* operator new[] (size_t)throw();
        void  operator delete[] (void* p);
        virtual ~TopClass() = 0;  // 纯虚函数,子类可以进行动态识别
    };
}

#endif
View Code

TopClass.cpp

#include<cstdlib>
#include"TopClass.h"

using namespace std;

namespace DSL
{
    void* TopClass::operator new (size_t size) throw() // C++11重写new操作符 返回值必须是void* 参数类型必须是size_t
    {
        return malloc(size);
    }
    void TopClass::operator delete (void *p)
    {
        free(p);
    }
    void* TopClass::operator new[] (size_t size) throw()
    {
        return malloc(size);
    }
    void TopClass::operator delete[] (void *p)
    {
        free(p);
    }
    TopClass::~TopClass() 
    {
    }
    
}
View Code

2. 智能指针类库

SmartPointer.h

/* 智能指针模板
 * 1.指针生命周期结束时主动释放堆空间 --> 析构函数
 * 2.一个堆空间只由一个指针指向        --> 拷贝构造函数,重载赋值符函数
 * 3.不可进行指针运算 -->
 * 4.重载解指针操作符,成员访问符  --> 重载->和*函数
 * 5.指针初始化为NULL --> 构造函数
 * 6.得到指针地址 --> get函数
 * 7.判断指针非空 --> isNULL函数
 */
 
#ifndef  SMARTPOINTER_H
#define  SMARTPOINTER_H

#include<iostream>
#include"TopClass.h"


namespace DSL
{
    template <typename T>
    class SmartPoniter : public TopClass
    {
    protected:
        T* pointer;
    public:
        SmartPoniter(T* p = NULL)   // 构造函数
        {
            pointer = p;
        }
        SmartPoniter(const SmartPoniter<T>& obj)  // 拷贝构造函数
        {
            pointer = obj.pointer;
            const_cast<SmartPoniter<T>&>(obj).pointer = NULL;
        }
        SmartPoniter<T>& operator = (const SmartPoniter<T>& obj)  //赋值符重载
        {
             if(this != &obj)
               {
                delete pointer;
                pointer = obj.pointer;
                const_cast<SmartPoniter<T>>(obj).pointer = NULL;
            }
            return this;
            
        }
        T* operator -> ()  // 成员访问重载
        {
            return pointer;
        }
        T& operator * ()  // 解指针重载
        {
            return *pointer;
        }
        T* get()    //得到指针变量值
        {
            return pointer;
        }
        bool isNULL()  // 判断指针是否为空
        {
            return (pointer == NULL);
        }
        ~SmartPoniter()  // 析构函数
        {
            delete pointer;
        }

    };
}

#endif
View Code

3. 异常类库

Exception.h

/*
*      Exception:  顶层异常抽象类   
*    ArithmeticException:       计算异常(除零)
*    NullPointerException:      空指针异常()
*    IdexOutOfBoundException:   越界异常(数组越界)
*    NotEnoughMemoryException: 内存不足异常( )
*    InvalidParameterException:参数错误异常(参数是否合法)
*    InvalidOperationException: 成员函数被调用时,其状态不正确则调用操作异常
*
*    Exception类:
*    功能:打印异常信息,打印异常地址
*    构造函数,析构函数,拷贝构造函数,赋值重载函数,信息打印函数,地址打印函数。
*    地址,信息
*
*/


#ifndef EXCEPTION_H    
#define EXCEPTION_H

#include<cstdlib>
#include"TopClass.h"

namespace DSL
{
#define THROW_EXCEPTION(obj,msg) (throw obj(msg,__FILE__,__LINE__)) // obj抛出异常的类型,msg抛出的异常信息
    class Exception : public TopClass
    {
    protected:
        char* pt_location;
        char* pt_message;
        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& obj);
        Exception& operator = (const Exception& obj);

        virtual const char* location()const;
        virtual const char* message()const;    
        
        virtual ~Exception() = 0;
    };

    class ArithmeticException: public Exception 
    {
    public:
        ArithmeticException():Exception(NULL){}
        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& obj):Exception(obj){}
        ArithmeticException& operator = (const ArithmeticException& obj)
        {
            Exception::operator=(obj);
            return *this;
        }
    };
        
    class NullPointerException: public Exception 
    {
    public:
        NullPointerException():Exception(NULL){}
        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& obj):Exception(obj){}
        NullPointerException& operator = (const NullPointerException& obj)
        {
            Exception::operator=(obj);
            return *this;
        }
    };
        
    class IdexOutOfBoundException: public Exception 
    {
    public:
        IdexOutOfBoundException():Exception(NULL){}
        IdexOutOfBoundException(const char* message):Exception(message){}
        IdexOutOfBoundException(const char* file,int line):Exception(file,line){}
        IdexOutOfBoundException(const char* message,const char* file,int line):Exception(message,file,line){}

        IdexOutOfBoundException(const IdexOutOfBoundException& obj):Exception(obj){}
        IdexOutOfBoundException& operator = (const IdexOutOfBoundException& obj)
        {
            Exception::operator=(obj);
            return *this;
        }
    };
                
    class NotEnoughMemoryException: public Exception 
    {
    public:
        NotEnoughMemoryException():Exception(NULL){}
        NotEnoughMemoryException(const char* message):Exception(message){}
        NotEnoughMemoryException(const char* file,int line):Exception(file,line){}
        NotEnoughMemoryException(const char* message,const char* file,int line):Exception(message,file,line){}

        NotEnoughMemoryException(const NotEnoughMemoryException& obj):Exception(obj){}
        NotEnoughMemoryException& operator = (const NotEnoughMemoryException& obj)
        {
            Exception::operator=(obj);
            return *this;
        }
    };
        
    class InvalidParameterException: public Exception 
    {
    public:
        InvalidParameterException():Exception(NULL){}
        InvalidParameterException(const char* message):Exception(message){}
        InvalidParameterException(const char* file,int line):Exception(file,line){}
        InvalidParameterException(const char* message,const char* file,int line):Exception(message,file,line){}

        InvalidParameterException(const InvalidParameterException& obj):Exception(obj){}
        InvalidParameterException& operator = (const InvalidParameterException& obj)
        {
            Exception::operator=(obj);
            return *this;
        }
    };
                
    class InvalidOperationException: public Exception 
    {
    public:
        InvalidOperationException():Exception(NULL){}
        InvalidOperationException(const char* message):Exception(message){}
        InvalidOperationException(const char* file,int line):Exception(file,line){}
        InvalidOperationException(const char* message,const char* file,int line):Exception(message,file,line){}

        InvalidOperationException(const InvalidOperationException& obj):Exception(obj){}
        InvalidOperationException& operator = (const InvalidOperationException& obj)
        {
            Exception::operator=(obj);
            return *this;
        }
    };
}

     
#endif
View Code

Exception.cpp

#include"Exception.h"
#include<cstring>
#include<cstdlib>
#include<cstdio>

using namespace std;

namespace DSL
{
    void Exception::init(const char * message, const char * file, int line)
    {
        pt_message = strdup(message); // strdup(const char *p)返回一个指针,这个指针指向p的一个复制串
        if(file != NULL)
        {
            char str[10];
            sprintf(str,"%d",line);
            pt_location = static_cast<char*>(malloc(sizeof(str) + sizeof(file) + 2));
            if(pt_location != NULL)

                {
                    pt_location = strcpy(pt_location, file);
                    pt_location = strcat(pt_location, ":");
                    pt_location = strcat(pt_location, str);
                }
        }
        else
        {
            pt_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& obj)
    {
        pt_location = strdup(obj.pt_location);
        pt_message = strdup(obj.pt_message);
    }

    Exception& Exception::operator = (const Exception& obj)
    {
        if(this != &obj)
        {
            free(obj.pt_location);
            free(obj.pt_message);
            pt_location = strdup(obj.pt_location);
            pt_message = strdup(obj.pt_message);
        }
        else
        {
            return *this;
        }

    }

    const char* Exception::location() const
    {
        return pt_location;
    }

    const char* Exception::message() const
     {
         return pt_message;
    }

    Exception::~Exception()
    {
        delete pt_location;
        delete pt_message;
    }

}
View Code
原文地址:https://www.cnblogs.com/zsy12138/p/10925962.html