当前位置:天才代写 > tutorial > C语言/C++ 教程 > C++中类模板(class template) 详解

C++中类模板(class template) 详解

2017-11-02 08:00 星期四 所属: C语言/C++ 教程 浏览:1375

类模板(class template)需要添加模板参数(template parameter), 即最前面添加"template <template T>";

把所有需要利用模板范例的位置, 利用"T"取代; 利用时需要填加"Class<T>",指定模板参数;

在界说类的成员函数(member function)时, 也需要添加类的模板参数"template <template T>",

而且在声明函数的归属类时, 类需要转换为模板类, 即"Class::"转换为"Class<T>::";

假如在类中, 假如利用本类工具, 即当前工具, 则可以不添加模板参数(添加也不没有问题);

其余留意初始化列表"initializer_list"的用法, 和前缀++或–与后缀++或–在重载时的区别;

代码如下:

/* 
 * cppprimer.cpp 
 * 
 *  Created on: 2013.11.21 
 *      Author: Caroline 
 */
      
/*eclipse cdt, gcc 4.8.1*/
      
#include <iostream>  
#include <vector>  
#include <initializer_list>  
#include <memory>  
#include <cstddef>  
      
template <typename T> class BlobPtr;  
template <typename T> class Blob;  
      
/*Blob: Binary Large OBject*/
template <typename T> class Blob {  
    friend class BlobPtr<T>;  
public:  
    typedef T value_type;  
    typedef typename std::vector<T>::size_type size_type;  
    Blob ();  
    Blob (std::initializer_list<T> il); //可以利用初始化列表, {}  
    size_type size() const { return data->size(); }  
    bool empty() const { return data->empty(); }  
    void push_back (const T &t) { data->push_back(t); }  
    void push_back (T &&t) { data->push_back(std::move(t)); } //右值操纵  
    void pop_back ();  
    T& back ();  
    T& operator[] (size_type i) ;  
private:  
    std::shared_ptr<std::vector<T> > data;  
    void check (size_type i, const std::string &msg) const; //验证给定的索引  
};  
      
template <typename T>  
Blob<T>::Blob () : data(std::make_shared<std::vector<T>>()) {}  
      
template <typename T>  
Blob<T>::Blob (std::initializer_list<T> il) :  
        data(std::make_shared< std::vector<T> >(il)) {}  
      
/*验证给定的索引*/
template <typename T>  
void Blob<T>::check (size_type i, const std::string &msg) const
{  
    if (i >= data->size())  
        throw std::out_of_range (msg); //抛出异常  
}  
      
template <typename T>  
T& Blob<T>::back ()  
{  
    check (0, "back on empty Blob");  
    return data->back ();  
}  
      
template <typename T>  
T& Blob<T>::operator[] (size_type i)  
{  
    check (i, "subscript out of range");  
    return (*data)[i];  
}  
      
template <typename T>  
void Blob<T>::pop_back ()  
{  
    check (0, "pop_back on empty Blob");  
    data->pob_back ();  
}  
      
template <typename T>  
class BlobPtr {  
public:  
    BlobPtr () : curr (0) {}  
    BlobPtr (Blob<T> &a, size_t sz=0) : wptr(a.data), curr (sz) {}  
    T& operator* () const {  
        auto p = check (curr, "dereference past end");  
        return (*p) [curr];  
    }  
    BlobPtr& operator++ (); //前缀操纵符  
    BlobPtr& operator-- ();  
    BlobPtr operator++ (int); //后缀操纵符  
    BlobPtr operator-- (int);  
private:  
    std::shared_ptr<std::vector<T>> check (std::size_t, const std::string&) const;  
    std::weak_ptr<std::vector<T>> wptr;  
    std::size_t curr;  
};  
      
template <typename T>  
std::shared_ptr<std::vector<T>>  
BlobPtr<T>::check (std::size_t i, const std::string& msg) const
{  
    auto ret = wptr.lock (); //判定wptr是否绑定了Blob  
    if (!ret)  
        throw std::runtime_error ("unbound BlobPtr");  
    if (i >= ret->size ())  
        throw std::out_of_range (msg);  
    return ret;  
}  
      
template <typename T>  
BlobPtr<T>& BlobPtr<T>::operator++ () {  
    check (curr, "increment past end of BlobPtr"); //先判定后加  
    ++curr;  
    return *this;  
}  
      
template <typename T>  
BlobPtr<T>& BlobPtr<T>::operator-- () {  
    --curr; //先减之后, 假如为0, 再减就是大整数  
    check (curr, "decrement past begin of BlobPtr"); //先减后判定  
    return *this;  
}  
      
template <typename T>  
BlobPtr<T> BlobPtr<T>::operator ++(int)  
{  
    BlobPtr ret = *this;  
    ++*this; //利用重载的前缀++  
    return ret;  
}  
      
template <typename T>  
BlobPtr<T> BlobPtr<T>::operator --(int)  
{  
    BlobPtr ret = *this;  
    --*this; //利用重载的前缀--  
    return ret;  
}  
      
int main (void) {  
    std::cout << "Hello Mystra!" << std::endl;  
    Blob<int> ia;  
    Blob<int> ia2 = {0, 1, 2, 3, 4};  
    std::cout << "ia2[2] = " << ia2[2] << std::endl;  
    BlobPtr<int> pia = ia2;  
    std::cout << "*(++pia) = " << *(++pia) << std::endl;  
    return 0;  
}

作者:csdn博客 Spike_King

 

    关键字:

天才代写-代写联系方式