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

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.

See Also

Reference

Class Templates

Member Function Templates

时间: 2024-08-30 12:45:05

Nested Class Templates的相关文章

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

c++中程序员使用模板能够写出与类型无关的代码,提高源代码重用,使用合适,大大提高了开发效率.此前,可以使用宏实现模板的功能,但是模板更加安全.清晰.在编写模板相关的代码是我们用到两个关键词:template和class(或者typename),class和typename基本没有区别. 1.函数模板 函数模板本质上就是实现针对不同类型的同一种算法的代码,其基本用法可以是形如: template <class TYPE ,class TYPE,......> ReturnValue funct

template(4.1)

5高阶基本技术(Tricky Basics) 本章涵盖实际编程之中层次较高的一些 template基本知识,包括关键词 typename的另一种用途.将member functio(n 成员函数)和 nested clas(s 嵌套类别)定为templates.奇特的 template template parameters.零值初始化(zero initialization).以字符串字面常数(string literals)作为 function templates arguments 的细

JsRender系列-11

<!DOCTYPE html> <html> <head> <script type="text/javascript" src="scripts/jquery.js"></script> <script type="text/javascript" src="scripts/jquery-ui.js"></script> <script

Django Templates

二分春色到穷阎,儿女祈翁出滞淹.幽蛰夜惊雷奋地,小窗朝爽日筛帘. 惠风全解墨池冻,清昼胜翻云笈签.亲友莫嗔情话少,向来屏息似龟蟾. 料峭寒犹薄,阴云带晚烟. 雨催惊蛰候,风作勒花开. 日永消香篆,愁浓逼酒船. 为君借余景,收拾赋新篇. 作者:小亲姐姐来源链接:http://www.qbaobei.com/jiaoyu/407464.html来源:亲亲宝贝著作权归作者所有. 料峭寒犹薄,阴云带晚烟. 雨催惊蛰候,风作勒花开. 日永消香篆,愁浓逼酒船. 为君借余景,收拾赋新篇. 作者:小亲姐姐来源链

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 failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.templateresolver.ServletContex

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

Spring Boot 项目,在 Spring Tool Suite 4, Version: 4.4.0.RELEASE 运行没有问题,将项目中的静态资源和页面复制到 IDEA 的项目中,除了 IDE 不同,其他基本相同. 运行 IDEA 中的项目,然后访问,出现异常: Exception processing template "index": An error happened during template parsing (template: "class path

Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]56528192: startup date [Tue Sep 19 15:05:24 CST 2017]; root of context hierarchy 2017-09-19 15:05:24.858 INFO 9986 --

nested exception is java.sql.SQLException: IO 错误

1.错误描述 (mx.messaging.messages::ErrorMessage)#0 body = (null) clientId = "18CE3B03-9709-9DA8-7634-340C23317FDD" correlationId = "3161DF25-D056-D694-E5CF-963E4FE036D9" destination = "waitController" extendedData = (null) faultC

Django的第一个Templates

1.URL配置方法: myblog中的urls.py 的文件 from django.conf.urls import url,include from django.contrib import admin import blog.views as bv urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', include('blog.urls')), ] blog中创建一个urls.py文件 from django.conf