zoukankan      html  css  js  c++  java
  • C++中的异常处理(下)

    C++中的异常处理(下)

     

     

     array.h

    复制代码
    #ifndef _ARRAY_H_
    #define _ARRAY_H_
    
    #include <stdexcept>
    
    using namespace std;
    
    template
    < typename T, int N >
    class Array
    {
        T m_array[N];
    public:
        int length() const;
        bool set(int index, T value);
        bool get(int index, T& value);
        T& operator[] (int index);
        T operator[] (int index) const;
        virtual ~Array();
    };
    
    template
    < typename T, int N >
    int Array<T, N>::length() const
    {
        return N;
    }
    
    template
    < typename T, int N >
    bool Array<T, N>::set(int index, T value)
    {
        bool ret = (0 <= index) && (index < N);
        
        if( ret )
        {
            m_array[index] = value;
        }
        
        return ret;
    }
    
    template
    < typename T, int N >
    bool Array<T, N>::get(int index, T& value)
    {
        bool ret = (0 <= index) && (index < N);
        
        if( ret )
        {
            value = m_array[index];
        }
        
        return ret;
    }
    
    template
    < typename T, int N >
    T& Array<T, N>::operator[] (int index)
    {
        if( (0 <= index) && (index < N) )
        {
            return m_array[index];
        }
        else
        {
            throw out_of_range("T& Array<T, N>::operator[] (int index)");
        }
    }
    
    template
    < typename T, int N >
    T Array<T, N>::operator[] (int index) const
    {
        if( (0 <= index) && (index < N) )
        {
            return m_array[index];
        }
        else
        {
            throw out_of_range("T Array<T, N>::operator[] (int index) const");
        }
    }
    
    template
    < typename T, int N >
    Array<T, N>::~Array()
    {
    
    }
    
    #endif
    复制代码

    HeapArray.h

    复制代码
    #ifndef _HEAPARRAY_H_
    #define _HEAPARRAY_H_
    
    #include <stdexcept>
    
    using namespace std;
    
    template
    < typename T >
    class HeapArray
    {
    private:
        int m_length;
        T* m_pointer;
        
        HeapArray(int len);
        HeapArray(const HeapArray<T>& obj);
        bool construct();
    public:
        static HeapArray<T>* NewInstance(int length); 
        int length() const;
        bool get(int index, T& value);
        bool set(int index ,T value);
        T& operator [] (int index);
        T operator [] (int index) const;
        HeapArray<T>& self();
        const HeapArray<T>& self() const;
        ~HeapArray();
    };
    
    template
    < typename T >
    HeapArray<T>::HeapArray(int len)
    {
        m_length = len;
    }
    
    template
    < typename T >
    bool HeapArray<T>::construct()
    {   
        m_pointer = new T[m_length];
        
        return m_pointer != NULL;
    }
    
    template
    < typename T >
    HeapArray<T>* HeapArray<T>::NewInstance(int length) 
    {
        HeapArray<T>* ret = new HeapArray<T>(length);
        
        if( !(ret && ret->construct()) ) 
        {
            delete ret;
            ret = 0;
        }
            
        return ret;
    }
    
    template
    < typename T >
    int HeapArray<T>::length() const
    {
        return m_length;
    }
    
    template
    < typename T >
    bool HeapArray<T>::get(int index, T& value)
    {
        bool ret = (0 <= index) && (index < length());
        
        if( ret )
        {
            value = m_pointer[index];
        }
        
        return ret;
    }
    
    template
    < typename T >
    bool HeapArray<T>::set(int index, T value)
    {
        bool ret = (0 <= index) && (index < length());
        
        if( ret )
        {
            m_pointer[index] = value;
        }
        
        return ret;
    }
    
    template
    < typename T >
    T& HeapArray<T>::operator [] (int index)
    {
        if( (0 <= index) && (index < length()) )
        {
            return m_pointer[index];
        }
        else
        {
            throw out_of_range("T& HeapArray<T>::operator [] (int index)");
        }
    }
    
    template
    < typename T >
    T HeapArray<T>::operator [] (int index) const
    {
        if( (0 <= index) && (index < length()) )
        {
            return m_pointer[index];
        }
        else
        {
            throw out_of_range("T HeapArray<T>::operator [] (int index) const");
        }
    }
    
    template
    < typename T >
    HeapArray<T>& HeapArray<T>::self()
    {
        return *this;
    }
    
    template
    < typename T >
    const HeapArray<T>& HeapArray<T>::self() const
    {
        return *this;
    }
    
    template
    < typename T >
    HeapArray<T>::~HeapArray()
    {
        delete[]m_pointer;
    }
    
    
    #endif
    复制代码
    复制代码
    #include <iostream>
    #include <string>
    #include "Array.h"
    #include "HeapArray.h"
    
    using namespace std;
    
    void TestArray()
    {
        Array<int, 5> a;
        
        for(int i=0; i<a.length(); i++)
        {
            a[i] = i;
        }
            
        for(int i=0; i<a.length(); i++)
        {
            cout << a[i] << endl;
        }
    }
    
    void TestHeapArray()
    {
        HeapArray<double>* pa = HeapArray<double>::NewInstance(5);
        
        if( pa != NULL )
        {
            HeapArray<double>& array = pa->self();
            
            for(int i=0; i<array.length(); i++)
            {
                array[i] = i;
            }
                
            for(int i=0; i<array.length(); i++)
            {
                cout << array[i] << endl;
            }
        }
        
        delete pa;
    }
    
    int main(int argc, char *argv[])
    {
        
        try
        {
            TestArray();
            
            cout << endl;
            
            TestHeapArray();
        }
        catch(...)
        {
            cout << "Exception" << endl;
        }
        
        return 0;
    }
    复制代码

  • 相关阅读:
    一种开源的分布式消息系统Nats
    资产盘点:除了金钱,一个人还有哪些资产?
    博客首页规则改版公告
    <html>
    欢迎使用CSDN-markdown编辑器
    java 小程序查看器 启动:未初始化小程序 解决方法
    Hadoop2.6.0版本号MapReudce演示样例之WordCount(一)
    深入学习IOZone【转】
    i.MX6UL -- PWM用户空间使用方法【转】
    linux PWM蜂鸣器移植以及驱动程序分析【转】
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14322332.html
Copyright © 2011-2022 走看看