子类继承时,访问父类的typedef问题

「已注销」 2016-01-21 09:47:38

template <class T1, class T2>
struct base1
{
typedef T1 first_type;
typedef T2 second_type;
};

template <class T1, class T2>
struct sub1 : public base1<T1, T2>
{
typename base1<T1, T2>::first_type a;
typename base1<T1, T2>::second_type b;
};

struct base2
{
typedef short first_type;
typedef long second_type;
};
struct sub2 : public base2
{
first_type a;
second_type b;
};

int main()
{
sub1<short, long> s1;
sub2 s2;

return 0;
}


sub1 必须指定 父类的first_type,而sub2直接就可以使用,请问二者的区别是什么?
...全文
339 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xulisb 2016-01-22
  • 打赏
  • 举报
回复
这跟typedef什么的没关系吧,模板类要给实际参数类型啊,你可以给first_type short 也可以给它int 这就是模板而已 你把typedef typename什么的删了,你还是要指定两个
paschen 版主 2016-01-22
  • 打赏
  • 举报
回复
引用 5 楼 BeTalker 的回复:
[quote=引用 2 楼 paschen 的回复:] base1中你需要用到T1 T2
请问下,哪本书里面有这方面的知识点。 c++ primer c++ programming language都没有找到[/quote] 《STL源码剖析》
「已注销」 2016-01-22
  • 打赏
  • 举报
回复
引用 2 楼 paschen 的回复:
base1中你需要用到T1 T2
请问下,哪本书里面有这方面的知识点。 c++ primer c++ programming language都没有找到
「已注销」 2016-01-22
  • 打赏
  • 举报
回复
引用 1 楼 D41D8CD98F 的回复:
差别:是否是dependent base class
两个都是dependent base class。。。
「已注销」 2016-01-22
  • 打赏
  • 举报
回复
引用 3 楼 Saleayas 的回复:
不需要 基类限制名的。 但是需要 typename 的。
去除 基类 就报错了。。。
「已注销」 2016-01-22
  • 打赏
  • 举报
回复
引用 10 楼 D41D8CD98F 的回复:
C++标准中,与模板相关的规定相当乱,所以这边就不引用了 这里,base1<T1, T2> 中的 T1 和 T2 都是模板参数,这样的类型是 dependent type 。这里,base1<T1, T2> 被用作基类,于是这种基类叫做 dependent base class 。按照 C++ 标准的规定,除非显式用 基类名 双冒号 成员名 的语法,否则不会查找 dependent base class 的成员。 虽然前面说了不引用C++标准,不过这最后一句的规定倒是能找到具体的段落,可以引用 N4567 § 14.6.2[temp.dep]p3 In the definition of a class or class template, the scope of a dependent base class (14.6.2.1) is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member. 之后有两个例子,后一个例子不引用了,反正也看不懂,前一个例子如下
typedef double A;
template<class T> class B {
    typedef int A;
};
template<class T> struct X : B<T> {
    A a; // a has type double
};
The type name A in the definition of X<T> binds to the typedef name defined in the global namespace scope, not to the typedef name defined in the base class B<T>. 而楼主例子中的 base2 和 sub2 跟模板没关系,自然不会碰上这条规则
奥。。多谢,明白了。。
Saleayas 2016-01-22
  • 打赏
  • 举报
回复
不需要 基类限制名的。 但是需要 typename 的。
D41D8CD98F 2016-01-22
  • 打赏
  • 举报
回复
C++标准中,与模板相关的规定相当乱,所以这边就不引用了 这里,base1<T1, T2> 中的 T1 和 T2 都是模板参数,这样的类型是 dependent type 。这里,base1<T1, T2> 被用作基类,于是这种基类叫做 dependent base class 。按照 C++ 标准的规定,除非显式用 基类名 双冒号 成员名 的语法,否则不会查找 dependent base class 的成员。 虽然前面说了不引用C++标准,不过这最后一句的规定倒是能找到具体的段落,可以引用 N4567 § 14.6.2[temp.dep]p3 In the definition of a class or class template, the scope of a dependent base class (14.6.2.1) is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member. 之后有两个例子,后一个例子不引用了,反正也看不懂,前一个例子如下
typedef double A;
template<class T> class B {
    typedef int A;
};
template<class T> struct X : B<T> {
    A a; // a has type double
};
The type name A in the definition of X<T> binds to the typedef name defined in the global namespace scope, not to the typedef name defined in the base class B<T>. 而楼主例子中的 base2 和 sub2 跟模板没关系,自然不会碰上这条规则
D41D8CD98F 2016-01-22
  • 打赏
  • 举报
回复
引用 4 楼 BeTalker 的回复:
[quote=引用 1 楼 D41D8CD98F 的回复:] 差别:是否是dependent base class
两个都是dependent base class。。。[/quote] 这东西不是您说了算的
paschen 版主 2016-01-21
  • 打赏
  • 举报
回复
base1中你需要用到T1 T2
D41D8CD98F 2016-01-21
  • 打赏
  • 举报
回复
差别:是否是dependent base class

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧