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. CactiEZ 中文版V10.1安装使用以及139邮箱短信报警设置

    说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合Cacti等相关软件,重新编译而成的一个操作系统!   说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合 ...

  2. 无法连接vCenter Server清单https://IP:10443

    VMware vCenter Server服务器安装系统的时候使用一个IP,安装完VMware vCenter后来更换了另外一个IP,当使用vSphere Web Client登陆VMware vCe ...

  3. 武汉科技大学ACM :1002: 零起点学算法66——反话连篇

    Problem Description 把输入的字符按照反着顺序输出 Input 多组测试数据  每组一行(每组数据不超过200个字符) Output 按照输入的顺序反着输出各个字符 Sample I ...

  4. [转]C++堆和栈的区别

    一.预备知识―程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)― 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. ...

  5. ubuntu11.10(TQ210)下移植boa服务器

    平台:ubuntu11.10 一.下载源码包www.boa.org   boa-0.94.13.tar.gz 二.解压,在其src目录下生产makefile #tar xvfz  boa-0.94.1 ...

  6. JQUERY1.9学习笔记 之基本过滤器(五) 大于选择器

    大于选择器:jQuery( ":gt(index)" )jQuery( ":gt(-index)" ) 例:大于TD5 到TD8 用黄色背景,TD8用红色文字. ...

  7. JS对undefined,null,NaN判断

    1.判断undefined: <span style="font-size: small;">var tmp = undefined; if (typeof(tmp) ...

  8. eclipse中JSP开发环境的配置

    1. Java环境 自行百度配置   2. Web Server环境安装: Web Server选择流行的Apache Tomcat .到http://tomcat.apache.org/  处下载, ...

  9. C 简单单元测试框架

    大约2年前,仿照GTEST写了个简单的C++单元测试框架. http://www.cnblogs.com/imlgc/archive/2012/02/09/2344506.html 后来用C写后台程序 ...

  10. 关于Bayes网络新解

    经典贝叶斯网络 贝叶斯分类器的分类原理是通过某对象的先验概率,利用贝叶斯公式计算出其后验概率,即该对象属于某一类的概率,选择具有最大后验概率的类作为该对象所属的类.目前研究较多的贝叶斯分类器主要有四种 ...