模板类的嵌套

来源:互联网 发布:淘宝卧室门帘 编辑:程序博客网 时间:2024/04/30 06:59
#include<iostream>using namespace std;template<class T>class A{public:struct B {B(T j): i(j) {}T i;}b;A(): b(1) {cout<<"A construct"<< endl;}B get() {    // 如果在B后面加<T>, 会导致错误,因为B并非特定模板类,虽然有T在其定义内,但他的T是随A的,在A中T属于已知范畴return b;}/*B<T> get() {return b;} */};template<class T>class C{public:template<class S>struct D{D(S j): i(j) {}S i;};  //不能在此直接加变量D<T> d;C(): d(5) {}D<T> get() {return d;}/* D get() {return d;} */};int main() {A<int> a;//A<int>::B<int> b(2);//A::B<int> b(2);A<int>::B b(2);C<int>::D<string> d("test");cout<< b.i<< endl;cout<< d.i<< endl;}
这个问题是我在做list类的iterator时的疑惑,总会有<T>的错误!
如果内部成员的是需要依赖于T的,那自己编的话不如就不要内嵌模板了,如果是想让他变成模板的话,要注意一点:
<span style="white-space:pre"></span>声明时前面加个 typename;
这个是在查问题时,发现的额外奖励,关于这点参考:http://blog.csdn.net/WaitForFree/article/details/10128771

0 0
原创粉丝点击