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 ...
随机推荐
- Android Studio中常用设置与快捷键
常用设置: 1.Tab不用4个空格Code Style->Java->Tabs and Indents->Use tab characterCode Style->Genera ...
- WinAPI——谐振动的合成
#include<Windows.h> #include<math.h> #define NUM 1000 #define PI 3.14159 LRESULT CALLBAC ...
- CentOS 6.4搭建zabbix
系统环境:CentOS 6.4 64bit Zabbix版本:zabbix 2.2.3 前提条件:已安装好LNMP环境 一.服务端: 1. 下载zabbix安装包zabbix-2.2.3.tar.g ...
- JavaScript 客户端JavaScript之 脚本化文档
客户端JavaScript的存在把静态HTML转变为交互式的Web应用程序,脚本化Web页面的内容正是JavaScript存在的理由. 一个文档对象模型或者说DOM就是一个API,它定义了如何访问 ...
- 给C++初学者的50个忠告(好文转载)
给C++初学者的50个忠告 1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3. ...
- JAVA-POI实现EXCEL的读写
想要完成JAVA读写EXCEL,首先需要JAVA-POI包的支持,百度搜索即可找到资源,不再赘述: POI-新增EXCEL并输入内容 package com.gsh.test.poi; import ...
- 1.3.1 switch 语句中的 String
switch语句是一种高效的多路语句,可以省掉很多繁杂的嵌套if判断: 在Java 6及之前,case语句中的常量只能是byte.char.short和int(也可以是对应的封装类)或枚举常量,在Ja ...
- webApi项目中的问题
1.场景:客户端调用API获取所有品牌列表,使用redis存储,第一次是获取全部,之后会增量获取,通过lasttime参数 出现的问题:redis连接超时,网络流量太大 原因:这个借口没做本地缓存,每 ...
- phpcms v9二次开发之模型类的应用(2)
二.模型操作方法select()--查询语句 //查询级别管理列表信息 public function levellists() { $lelists = $this->l ...
- js 获取节点
var chils= s.childNodes; //得到s的全部子节点 var par=s.parentNode; //得到s的父节点 var ns=s.nextSbiling; //获得s的下一个 ...