博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nested Class Templates
阅读量:5242 次
发布时间:2019-06-14

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

 

 

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 .

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 
using namespace std;class X{ template
struct Y { T m_t; Y(T t): m_t(t) { } }; Y
yInt; Y
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 
using namespace std;template
class X{ template
class Y { U* u; public: Y(); U& Value(); void print(); ~Y(); }; Y
y;public: X(T t) { y.Value() = t; } void print() { y.print(); }};template
template
X
::Y
::Y(){ cout << "X
::Y
::Y()" << endl; u = new U();}template
template
U& X
::Y
::Value(){ return *u;}template
template
void X
::Y
::print(){ cout << this->Value() << endl;}template
template
X
::Y
::~Y(){ cout << "X
::Y
::~Y()" << endl; delete u;}int main(){ X
* xi = new X
(10); X
* xc = new X
('c'); xi->print(); xc->print(); delete xi; delete xc;}

 
 
 
1 a

 
 
 
X
::Y
::Y()X
::Y
::Y()1099X
::Y
::~Y()X
::Y
::~Y()

Local classes are not allowed to have member templates.

 

Reference

转载于:https://www.cnblogs.com/lvdongjie/p/4489918.html

你可能感兴趣的文章
python学习笔记
查看>>
php+ajax(jquery)的文件异步上传
查看>>
使用&nbsp;SharedPreferences 分类: Andro...
查看>>
TLA+(待续...)
查看>>
python selenium 基本常用操作
查看>>
题解: [GXOI/GZOI2019]与或和
查看>>
Eurekalog
查看>>
LeetCode--169--求众数
查看>>
Copy 函数
查看>>
Android服务之Service(其一)
查看>>
网站sqlserver提权操作
查看>>
PHP变量作用域以及地址引用问题
查看>>
实验四
查看>>
Elastic Stack-Elasticsearch使用介绍(三)
查看>>
MacOS copy图标shell脚本
查看>>
【索引】gtest学习笔记
查看>>
第八章 方法
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>