博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 定义类模板
阅读量:4088 次
发布时间:2019-05-25

本文共 1727 字,大约阅读时间需要 5 分钟。

16.1.2. Defining a Class Template

template <class Type> class Queue {

public:
Queue (); // default constructor
Type &front (); // return element from head of Queue
const Type &front () const;
void push (const Type &); // add element to back of Queue
void pop(); // remove element from head of Queue
bool empty() const; // true if no elements in the Queue
private:
// ...
};

A  class template is a template, so it must begin with the keyword templatefollowed by a template parameter list. Our Queuetemplate takes a single template type parameter named Type.

With the exception of the template parameter list, the definition of a class template looks like any other class. A class template may define data, function, and type members; it may use access labels to control access to those members; it defines constructors and destructors; and so on.In the definition of the class and its members, we can use the template parameters as stand-ins for types or values that will be supplied when the class is used.

Using a Class Template

In contrast to calling a function template, when we use a class template, we must explicitly specify arguments for the template parameters:

Queue<int> qi; // Queue that holdsints
Queue< vector<double> > qc; // Queue that holds vectors of doubles
Queue<string> qs; // Queue that holds strings

The compiler uses the arguments to instantiate a type-specific version of the class. Essentially,

the compiler rewrites our Queueclass replacing Typeby the specified actual type provided by the
user. In this case, the compiler will instantiate three classes: a version of Queuewith Type
replaced by int, a second Queueclass that uses vector<double>in place of  Type, and a third
that replaces  Typeby string.

转载地址:http://gyyii.baihongyu.com/

你可能感兴趣的文章
Flutter 组件通信(父子、兄弟)
查看>>
Flutter Animation动画
查看>>
Flutter 全局监听路由堆栈变化
查看>>
Android 混合Flutter之产物集成方式
查看>>
Flutter混合开发二-FlutterBoost使用介绍
查看>>
Flutter 混合开发框架模式探索
查看>>
Flutter 核心原理与混合开发模式
查看>>
Flutter Boost的router管理
查看>>
Android Flutter混合编译
查看>>
微信小程序 Audio API
查看>>
[React Native]react-native-scrollable-tab-view(进阶篇)
查看>>
Vue全家桶+Mint-Ui打造高仿QQMusic,搭配详细说明
查看>>
React Native for Android 发布独立的安装包
查看>>
React Native应用部署/热更新-CodePush最新集成总结(新)
查看>>
react-native-wechat
查看>>
基于云信的react-native聊天系统
查看>>
网易云音乐移动客户端Vue.js
查看>>
ES7 await/async
查看>>
ES7的Async/Await
查看>>
React Native WebView组件实现的BarCode(条形码)、(QRCode)二维码
查看>>