嵌套模版类
来源:互联网 发布:长沙最新cnc编程招聘 编辑:程序博客网 时间:2024/06/10 19:02
Templates can be defined within classes or class templates, in which case they are referred to as member templates. Member templates that are classes are referred to as nested class templates. Member templates that are functions are discussed in Member Function Templates.
Nested class templates are declared as class templates inside the scope of the outer class. They can be defined inside or outside of the enclosing class.
The following code demonstrates a nested class template inside an ordinary class.
// nested_class_template1.cpp// compile with: /EHsc#include <iostream>using namespace std;class X{ template <class T> struct Y { T m_t; Y(T t): m_t(t) { } }; Y<int> yInt; Y<char> yChar;public: X(int i, char c) : yInt(i), yChar(c) { } void print() { cout << yInt.m_t << " " << yChar.m_t << endl; }};int main(){ X x(1, 'a'); x.print();}
When nested class templates are defined outside of their enclosing class, they must be prefaced by the template parameters for both the class template (if they are members of a class template) and template parameters for the member template.
// nested_class_template2.cpp// compile with: /EHsc#include <iostream>using namespace std;template <class T>class X{ template <class U> class Y { U* u; public: Y(); U& Value(); void print(); ~Y(); }; Y<int> y;public: X(T t) { y.Value() = t; } void print() { y.print(); }};template <class T> template <class U>X<T>::Y<U>::Y(){ cout << "X<T>::Y<U>::Y()" << endl; u = new U();}template <class T> template <class U>U& X<T>::Y<U>::Value(){ return *u;}template <class T> template <class U>void X<T>::Y<U>::print(){ cout << this->Value() << endl;}template <class T> template <class U>X<T>::Y<U>::~Y(){ cout << "X<T>::Y<U>::~Y()" << endl; delete u;}int main(){ X<int>* xi = new X<int>(10); X<char>* xc = new X<char>('c'); xi->print(); xc->print(); delete xi; delete xc;}
- 嵌套模版类
- 模版类中嵌套自定义模版类型
- C++模版-重载,嵌套
- 模版——函数模版、类模版
- 模版类
- 模版类
- 类模版
- 模版类
- 类模版
- 类模版
- 类模版
- 类模版
- 类模版
- 类模版中函数模版
- 函数模版与类模版
- 函数模版和类模版
- 模版笔记002 - 类模版
- 函数模版和类模版
- MySpace,YouTube,Flickr等各大网站架构总结
- mp4文件格式解析(一)
- wince6.0驱动开发
- libjpeg的交叉编译
- mp4文件格式解析(二)
- 嵌套模版类
- 声音设备返回的input buffer总是320个字节
- 九度OJ 题目1041:Simple Sorting
- mp4文件格式解析(三)
- iText生成PDF入门
- mp4文件格式解析(四)
- Dos文件到Unix文件转换
- 各种数据类型的取值范围
- 入网与入库的学习