官术网_书友最值得收藏!

Syntax

The syntax for creating a class template is as follows:

[export] template <template_parameter_list> class-declaration 

Here, we have the following:

  • template_parameter-list (see the link in further reading context [10]) is a non-empty comma-separated list of the template parameters, each of which is either a non-type parameter, a type parameter, a template parameter, or a parameter pack of any of those.
  • class-declaration is the part that's used to declare a class that contains a class name and its body in curly brackets. By doing so, the declared class name also becomes a template name.

For example, we can define a class template, V, so that it contains all kinds of 1D data types:

template <class T>
class V {
public:
V( int n = 0) : m_nEle(n), m_buf(0) { creatBuf();}
~V(){ deleteBuf(); }
V& operator = (const V &rhs) { /* ... */}
V& operator = (const V &rhs) { /* ... */}
T getMax(){ /* ... */ }
protected:
void creatBuf() { /* ... */}
void deleteBuf(){ /* ... */}

public:
int m_nEle;
T * m_buf;
};

Once we have this class template, the compiler can generate classes during the instantiation process. For the reason we mentioned in the Function template subsection, we will avoid using the imprecise term template class in this book. Instead, we will use the class template.

主站蜘蛛池模板: 芮城县| 永胜县| 永城市| 太谷县| 界首市| 福建省| 呼玛县| 武安市| 沾益县| 北碚区| 兴山县| 海伦市| 明星| 麻江县| 鞍山市| 南雄市| 固阳县| 平塘县| 阿瓦提县| 仙桃市| 静乐县| 莒南县| 溧水县| 长沙县| 报价| 陆河县| 开原市| 灌南县| 库尔勒市| 永清县| 安陆市| 鹤山市| 衡东县| 二连浩特市| 渑池县| 新乡市| 资溪县| 栾川县| 利辛县| 昌邑市| 灵璧县|