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 ...
随机推荐
- Jquery.Sorttable 桌面拖拽自定义
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 关于oracle dbms_job 定时执行的内容。
一.设置初始化参数 job_queue_processessql> alter system set job_queue_processes=n;(n>0)job_queue_proces ...
- 编程语言大牛王垠:编程的智慧,带你少走弯路 [本文转载CocoaChina]
作者:王垠 授权本站转载. 编程是一件创造性的工作,是一门艺术.精通任何一门艺术,都需要很多的练习和领悟,所以这里提出的“智慧”,并不是号称三天瘦二十斤的减肥药,它并不能代替你自己的勤奋.然而我希望它 ...
- JavaScript 客户端JavaScript之样式表操作(DOM API 提供模块之一)
层叠样式 表和动态HTML 层叠样式表(CSS)是指定HTML文档或XML文档的表现的标准. 使用CSS和Javascript,可以创建出各种视觉效果,这些效果可以统称为动态HTML(DH ...
- 【C#学习笔记】二、面向对象编程
2.1 抽象类与接口 1)概念 抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类所不能的.抽象方法只能声明于抽象类中,且不包含任何实现,派生类 ...
- poj1204之AC自动机
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8235 Accepted: 3104 Sp ...
- 添加view类图中的二级菜单
void CFafdsafasdfasfasView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message han ...
- jquery keypress事件浏览器兼容性
今天在做“财务管理系统”的时候,使用jquery的ajax从前台传递用户输入到后台,并保存到数据库,但是在前台为了界面的简介和一致性,没有使用按 钮来实现"确定"和"取消 ...
- How to install ffmpeg,mp4box,mplayer,mencoder,flvtool2,ffmpeg-php on centos
1. Enable RPM Fusion yum repository The CentOS rpm packages of ffmpeg, mplayer, mencoder and MP4Box ...
- Swift—final关键字-b
在类的定义中使用final关键字声明类.属性.方法和下标.final声明的类不能被继承,final声明的属性.方法和下标不能被重写. 下面看一个示例: final class Person { //声 ...