注册 登录
编程论坛 C++教室

类模板的定义

反脑控2016 发布于 2016-09-02 17:46, 1688 次点击
template<class T>
struct node
{
    T  a;
    struct node*p;
    struct node<T>*r;
};
我想问:
p与r的定义完全一样吗?
4 回复
#2
rjsp2016-09-02 22:48
你可以用typeid在运行时比较
还可以用p=r在编译时看它报不报错
#3
反脑控20162016-09-03 03:37
感谢版主的提醒,通过下面的例子,得出是等效的:
#include<iostream>
using namespace std;
template <class T>
struct node
{
    int data;
    node*p;
    node<T>*r;
};
 void main()
{
    node<int> one;
    cout<<typeid(one.p).name()<<endl;
    cout<<typeid(one.r).name()<<endl;
    node<double>two;
    cout<<typeid(two.p).name()<<endl;
    cout<<typeid(two.r).name()<<endl;
}
#4
rjsp2016-09-03 10:53
回复 3楼 反脑控2016
不能比较名字
因为标准对名字没有任何规定,甚至所有类型叫同一个名字都是符合标准的
#5
反脑控20162016-09-03 11:27
多谢。我看不懂原版的MSDN,只能看看帖子,编编小程序测试测试而已,不知道标准。看样子我是遇见了大才,以后请多多赐教。
1