我想从子类中的基类中提供对现有类型的访问。
我发现了两种不同的方法:
struct A {
typedef int mytype;
};
struct B {
typedef double mytype;
};
我可以“包括”带有using声明的类型:
struct C : A, B {
using typename A::mytype;
};
或者我可以创建一个类型别名:
struct C : A, B {
typedef A::mytype mytype;
using mytype = A::mytype; //C++11
};
谢谢。
相关问题:Using-declaration of an existing namespace type vs creating a type alias
发布于 2019-10-18 12:23:52
是有区别的。如果您的结构A和B被定义为:
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;
,因为它不太含糊。
https://stackoverflow.com/questions/58449034
复制相似问题