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

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.

主站蜘蛛池模板: 四平市| 鄂尔多斯市| 衢州市| 田阳县| 磐石市| 梁河县| 凌海市| 大竹县| 交口县| 丰都县| 乌拉特中旗| 平舆县| 乐至县| 府谷县| 阜新| 苏尼特右旗| 诸暨市| 十堰市| 浦县| 宣汉县| 泸溪县| 周宁县| 宁城县| 华安县| 濉溪县| 惠安县| 乌鲁木齐县| 余庆县| 苍溪县| 乌拉特前旗| 江孜县| 明溪县| 巫溪县| 东乌珠穆沁旗| 尚义县| 新龙县| 平阳县| 新绛县| 山阳县| 利津县| 清涧县|