使用基类中现有类型的声明与在子类中创建类型别名 [英] Using-declaration of an existing type from base class vs creating a type alias inside child class
本文介绍了使用基类中现有类型的声明与在子类中创建类型别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想提供子类中基类对现有类型的访问.
I would like to provide access to an existing type from base class inside child class.
我发现了两种不同的方式:
I found two different ways :
struct A {
typedef int mytype;
};
struct B {
typedef double mytype;
};
我可以使用using声明包含"类型:
I can "include" the type with a using declaration :
struct C : A, B {
using typename A::mytype;
};
或者我可以创建一个类型别名:
Or I can create a type alias :
struct C : A, B {
typedef A::mytype mytype;
using mytype = A::mytype; //C++11
};
- 有什么区别吗?
- 每种语法的优缺点是什么?
- 哪个是最常用/推荐的?
谢谢.
相关问题:使用-声明现有名称空间类型与创建类型别名
推荐答案
有区别.考虑一下,如果将结构A和B定义为:
There is a difference. Consider what would happen if your structs A and B were defined as:
struct A {
protected:
int mytype;
};
struct B {
protected:
double mytype;
};
在这种情况下
struct C : A, B {
using typename A::mytype; // Would compile, but is mytype a type or
// an exposed member of the base class?
//using mytype = A::mytype; // Would not compile
};
在您的情况下,我建议您使用using mytype = A::mytype;
,因为它不太含糊.
In your case I would recommend using using mytype = A::mytype;
as it is less ambiguous.
这篇关于使用基类中现有类型的声明与在子类中创建类型别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文