64,685
社区成员
发帖
与我相关
我的任务
分享
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;
}
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 跟模板没关系,自然不会碰上这条规则