C++ 模板基类中枚举的typedef
继昨晚的一个答案之后——我希望更多的评论能为我解答这个问题,但这是毫无疑问的 有没有一种方法可以在不继承的情况下实现这一点,而不需要在下面的倒数第二行代码中使用繁琐的用法,它将值写入C++ 模板基类中枚举的typedef,c++,visual-c++,templates,inheritance,enums,C++,Visual C++,Templates,Inheritance,Enums,继昨晚的一个答案之后——我希望更多的评论能为我解答这个问题,但这是毫无疑问的 有没有一种方法可以在不继承的情况下实现这一点,而不需要在下面的倒数第二行代码中使用繁琐的用法,它将值写入cout struct A { enum E { X, Y, Z }; }; template <class T> struct B { typedef typename T::E E; }; // basically "import" the A::E enu
cout
struct A {
enum E {
X, Y, Z
};
};
template <class T>
struct B {
typedef typename T::E E;
};
// basically "import" the A::E enum into B.
int main(void)
{
std::cout << B<A>::E::X << std::endl;
return 0;
}
结构A{
枚举E{
十、 Y,Z
};
};
样板
结构B{
typedef typename T::E;
};
//基本上是将A::E枚举“导入”到B中。
内部主(空)
{
这有帮助吗
struct A {
enum E {
X, Y, Z
};
};
template <class T>
struct B : private T{ // private inheritance.
public:
using T::X;
};
// basically "import" the A::E enum into B.
int main(void)
{
B<A>::X; // Simpler now?
return 0;
}
结构A{
枚举E{
十、 Y,Z
};
};
样板
结构B:私有T{//私有继承。
公众:
使用T::X;
};
//基本上是将A::E枚举“导入”到B中。
内部主(空)
{
B::X;//现在更简单了吗?
返回0;
}
将名称
enum
值名称直接放入类中的唯一方法是从具有这些名称的类继承
您显示的代码似乎使用了Microsoft语言扩展
在C++98中,enum
typename不能用于限定其中一个值名:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 17: error: name followed by "::" must be a class or namespace
name... Wild guess: Did you #include the right header?
std::cout << B<A>::E::X << std::endl;
^
1 error detected in the compilation of "ComeauTest.c".
…做
typedef T E;
Cheers&hth.,当我进行建议的更改时,
cout
行未编译。错误C2039:“X”:不是具有[T=a]的“B”的成员@Steve:不,它应该与原始用法一起工作(只是更改了typedef).如果你想让值X
无条件地直接可用,你必须继承它们。就像Steve的回答一样。但我认为最终你不会想这么做。“最终”→ 也许几年后…;-)干杯&嗯。没有继承权有什么办法可以做到这一点吗?为什么,私人继承会引起什么问题?
typedef T E;