sarray2.hpp

来自「C++ Templates 完全导引范例」· HPP 代码 · 共 79 行

HPP
79
字号
/* The following code example is taken from the book * "C++ Templates - The Complete Guide" * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002 * * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */#include <stddef.h>#include <cassert>template<typename T>class SArray {  public:    // create array with initial size    explicit SArray (size_t s)     : storage(new T[s]), storage_size(s) {        init();    }    // copy constructor    SArray (SArray<T> const& orig)     : storage(new T[orig.size()]), storage_size(orig.size()) {        copy(orig);    }    // destructor: free memory    ~SArray() {        delete[] storage;    }    // assignment operator    SArray<T>& operator= (SArray<T> const& orig) {        if (&orig!=this) {            copy(orig);        }        return *this;    }    // return size    size_t size() const {        return storage_size;    }    // index operator for constants and variables    T operator[] (size_t idx) const {        return storage[idx];    }    T& operator[] (size_t idx) {        return storage[idx];    }  protected:    // init values with default constructor    void init() {        for (size_t idx = 0; idx<size(); ++idx) {            storage[idx] = T();        }    }    // copy values of another array    void copy (SArray<T> const& orig) {        assert(size()==orig.size());        for (size_t idx = 0; idx<size(); ++idx) {            storage[idx] = orig.storage[idx];        }    }  private:    T*     storage;       // storage of the elements    size_t storage_size;  // number of elements  public:    SArray<T>& operator+= (SArray<T> const& b);    SArray<T>& operator*= (SArray<T> const& b);    SArray<T>& operator*= (T const& s);};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?