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.

Example

 

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;
}

Output

 
 
 
1 a

Output

 
 
 
X<T>::Y<U>::Y()
X<T>::Y<U>::Y()
10
99
X<T>::Y<U>::~Y()
X<T>::Y<U>::~Y()

Local classes are not allowed to have member templates.

Nested Class Templates的更多相关文章

  1. 我也来谈一谈c++模板(一)

    c++中程序员使用模板能够写出与类型无关的代码,提高源代码重用,使用合适,大大提高了开发效率.此前,可以使用宏实现模板的功能,但是模板更加安全.清晰.在编写模板相关的代码是我们用到两个关键词:temp ...

  2. JsRender系列-11

    <!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...

  3. systemtap 2.8 news

    * What's new in version 2.8, 2015-06-17 - SystemTap has improved support for probing golang programs ...

  4. Using FreeMarker templates (FTL)- Tutorial

    Lars Vogel, (c) 2012, 2016 vogella GmbHVersion 1.4,06.10.2016 Table of Contents 1. Introduction to F ...

  5. eclipse的xml文件提示templates的模板.md

    eclipse的xml文件提示templates的模板 <?xml version="1.0" encoding="UTF-8" standalone=& ...

  6. confd template src格式和 templates 语法

    Template Resources Template resources are written in TOML and define a single template resource. Tem ...

  7. <Effective C++>读书摘要--Templates and Generic Programming<一>

    1.The initial motivation for C++ templates was straightforward: to make it possible to create type-s ...

  8. Spring MVC集成thymeleaf时提示:defined in ServletContext resource [/WEB-INF/SrpingMVCTest-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException

    错误提示: defined in ServletContext resource [/WEB-INF/SrpingMVCTest-servlet.xml]: Instantiation of bean ...

  9. Thymeleaf 异常:Exception processing template "index": An error happened during template parsing (template: "class path resource [templates/index.html]")

    Spring Boot 项目,在 Spring Tool Suite 4, Version: 4.4.0.RELEASE 运行没有问题,将项目中的静态资源和页面复制到 IDEA 的项目中,除了 IDE ...

随机推荐

  1. https加密

    对称加密  客户端和服务器使用同一把钥匙,加密算法公开 非对称加密  不同钥匙,公钥加密的私钥可以打开 私钥加密的公钥可以打开 HTTPS关键: 1. 要传输的业务数据,使用对称加密. 客户端生成私钥 ...

  2. MySQL查看数据库大小、表大小和最后修改时间

    查看数据库表基本信息. select * from information_schema.TABLES where information_schema.TABLES.TABLE_SCHEMA = ' ...

  3. JavaScript Arguments 实现可变参数的函数,以及函数的递归调用

    //可变参数的函数 注:也可以使用对象作为参数来实现 function Max() { var temp = arguments[0] || 0; for (var i = 1; i < arg ...

  4. ValidateCode.cs验证码时设置缓存的使用

    using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...

  5. centos 6.5 安装阿里云的一键安装包(nginx+php5.4+mysql5.1)

    安装阿里云提供的Linux一键安装web环境全攻略,本想着会有最复杂 ,没想到阿里云工程师提供的包没有任何限制(开始以为只能在阿里去的主机上使用).开源的精神就是好(注:我是伸手党). 环境  vmw ...

  6. python中的列表和字典

    列表和字典的区别: 列表是有序排列的一些物件,而字典是将一些物件(键)对应到另外一些物件(值)的数据结构; 应用场景: 字典 各种需要通过某个值去查看另一个值的场合,也就是一个虚拟的“查询表”,实现方 ...

  7. 学c语言做练习

    /*编写一个函数,其功能是使输入字符串反序.在一个使用循环语句为这个函数提供输入的完整 程序中进行测试.*/ #include<stdio.h> #include<string.h& ...

  8. mysql 索引管理原则

    最近在学习mysql的索引优化,结合着我们网盟系统的一些业务,翻阅一些资料,整理出如下的一些想法: 1.索引建立的原则一:最左前缀匹配原则 ,非常重要的原则,mysql会一直向右匹配直到遇到范围查询( ...

  9. d3可视化实战01:理解SVG元素特性

    一. SVG简介 ————————————————————————————————————————————————————————————————— SVG是一种和图像分辨率无关的矢量图形格式,它使用 ...

  10. Entity Framework with MySQL 学习笔记一(关系整理版)

    1-1 设置 //DataAnnotation 1-1 | 1-0 table //SQLtable : member , columns : memberId, name //SQL basic l ...