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

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.

主站蜘蛛池模板: 菏泽市| 渭南市| 溧水县| 建昌县| 茶陵县| 巴中市| 新田县| 东方市| 翁牛特旗| 龙井市| 黄大仙区| 边坝县| 镇江市| 湘潭县| 渭南市| 手游| 温州市| 筠连县| 镇安县| 行唐县| 丹凤县| 巴彦淖尔市| 韶山市| 融水| 济源市| 南漳县| 监利县| 饶平县| 怀安县| 新竹市| 临桂县| 孙吴县| 武强县| 监利县| 萝北县| 双辽市| 甘泉县| 樟树市| 化德县| 井研县| 浦城县|