- 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.
推薦閱讀
- Getting Started with Citrix XenApp? 7.6
- Instant Typeahead.js
- Learning Laravel 4 Application Development
- Big Data Analytics
- Ext JS 4 Web Application Development Cookbook
- Tableau 10 Bootcamp
- 零基礎(chǔ)Java學(xué)習(xí)筆記
- Creating Data Stories with Tableau Public
- Learning JavaScript Data Structures and Algorithms(Second Edition)
- Java程序設(shè)計(jì)基礎(chǔ)(第6版)
- C語(yǔ)言從入門(mén)到精通
- Scrapy網(wǎng)絡(luò)爬蟲(chóng)實(shí)戰(zhàn)
- Android高級(jí)開(kāi)發(fā)實(shí)戰(zhàn):UI、NDK與安全
- Google Adsense優(yōu)化實(shí)戰(zhàn)
- Python滲透測(cè)試編程技術(shù):方法與實(shí)踐(第2版)