- Expert C++
- Vardan Grigoryan Shunguang Wu
- 221字
- 2021-06-24 16:34:07
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.
推薦閱讀
- 深入理解Bootstrap
- 摩登創(chuàng)客:與智能手機和平板電腦共舞
- Practical Internet of Things Security
- AWS Serverless架構(gòu):使用AWS從傳統(tǒng)部署方式向Serverless架構(gòu)遷移
- 青少年軟件編程基礎(chǔ)與實戰(zhàn)(圖形化編程三級)
- Java從入門到精通(第5版)
- Python王者歸來
- 持續(xù)輕量級Java EE開發(fā):編寫可測試的代碼
- Flowable流程引擎實戰(zhàn)
- Hands-On Full Stack Development with Spring Boot 2.0 and React
- RocketMQ實戰(zhàn)與原理解析
- Ionic3與CodePush初探:支持跨平臺與熱更新的App開發(fā)技術(shù)
- Learning Apache Thrift
- 面向?qū)ο蠓治雠c設(shè)計(第3版)
- Office VBA開發(fā)經(jīng)典:中級進階卷