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

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.

主站蜘蛛池模板: 辽宁省| SHOW| 丹东市| 林甸县| 丹棱县| 如东县| 徐闻县| 景泰县| 武穴市| 屯门区| 白银市| 葫芦岛市| 招远市| 高雄市| 准格尔旗| 紫云| 特克斯县| 辽宁省| 高要市| 白银市| 禄劝| 隆昌县| 区。| 苏尼特右旗| 锡林郭勒盟| 余姚市| 怀远县| 安岳县| 潜山县| 北票市| 民县| 海阳市| 七台河市| 阿荣旗| 辽中县| 乌兰察布市| 望都县| 永嘉县| 高邮市| 山阴县| 巴青县|