Nested Class Templates
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
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的更多相关文章
- 我也来谈一谈c++模板(一)
		c++中程序员使用模板能够写出与类型无关的代码,提高源代码重用,使用合适,大大提高了开发效率.此前,可以使用宏实现模板的功能,但是模板更加安全.清晰.在编写模板相关的代码是我们用到两个关键词:temp ... 
- JsRender系列-11
		<!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ... 
- systemtap 2.8 news
		* What's new in version 2.8, 2015-06-17 - SystemTap has improved support for probing golang programs ... 
- Using FreeMarker templates (FTL)- Tutorial
		Lars Vogel, (c) 2012, 2016 vogella GmbHVersion 1.4,06.10.2016 Table of Contents 1. Introduction to F ... 
- eclipse的xml文件提示templates的模板.md
		eclipse的xml文件提示templates的模板 <?xml version="1.0" encoding="UTF-8" standalone=& ... 
- confd template src格式和 templates 语法
		Template Resources Template resources are written in TOML and define a single template resource. Tem ... 
- <Effective C++>读书摘要--Templates and Generic Programming<一>
		1.The initial motivation for C++ templates was straightforward: to make it possible to create type-s ... 
- 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 ... 
- 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 ... 
随机推荐
- GridView下DropDownList 的选择方法onselectedindexchanged 实现方法
			在GridView下面绑定好了下拉框,我们常常会遇到一个问题, 选择方法怎么实现呢,用js总是难的去算是在GridView的第几行第几个元素,因为服务器的id和客户端的id经常变化让js根本无从找起, ... 
- android 广播分类
			安卓广播分为两类:1.普通广播, broadcast,广播发出之后所有满足条件的应用都能获取到广播里面的数据,缺点是应用获取广播中的数据修改之后不能传递给其它接收广播的应用:2.有序广播,orderb ... 
- JavaScript 之 使用 XMLHttpRequest 上传文件
			<div id="div1"> <input type="file" id="uploadfile" style=&quo ... 
- ibatis.net调用oracle存储过返回游标SYS_REFCURSOR结果集
			最近在用ibatis.net框架和oracle 11g开发一套程序.其中有一个需求就是通过存储过程,查询指定条件的数据集. 但是在开发的过程中遇到了问题,问题如下: 1.如何通过ibatis.net执 ... 
- PHPCMS V9网站更换域名的方法
			网站在发展的过程中,很可能多次的修改域名.那么在phpcms v9中我们要怎么进行设置呢? 请进行以下步骤的修改: 1.修改/caches/configs/system.php里面所有和域名有关的,把 ... 
- dede仿站笔记
			仿站步骤查看是否为dedecms的方法,看引用路径src="/templets/default2012/images/toutiao.png" 0查看仿站编码,选择utf8或gbk ... 
- php如何做数据库攻击
			PHP mysql_real_escape_string() 函数 PHP MySQL 函数 定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的 ... 
- 全排列算法之Perm算法实现
			题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' < 'b' < … < 'y' < 'z',而且给定的字符 ... 
- C++构造函数 & 拷贝构造函数 & 派生类的构造函数 & 虚继承的构造函数
			构造函数 ,是一种特殊的方法 .主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 .特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数 ... 
- 转:内核中的内存申请:kmalloc、vmalloc、kzalloc、kcalloc、get_free_pages
			在内核模块中申请分配内存需要使用内核中的专用API:kmalloc.vmalloc.kzalloc.kcalloc.get_free_pages;当然,设备驱动程序也不例外;对于提供了MMU功能的处理 ... 
