C ++前向声明类范围的枚举 [英] C++ Forward declaring class scoped enumeration
问题描述
我想知道是否可以转发声明另一个类范围内定义的枚举。例如,考虑以下内容:
I'm wondering if it's possible to forward declare an enum that's defined within another class scope. For example, consider the following:
//A.h
class A
{
public:
enum class type: unsigned long { /*some stuff*/ }
};
现在,在另一个标头中,我想转发声明 type枚举(假设类B下面的函数可以对A :: type进行操作)
Now, in another header I'd like to forward declare the 'type' enum (suppose class B below has a function that does something with A::type)
//B.h
enum A::type; //use of undefined type 'A'
class B
{
public:
UseTypeEnum(A::Type&);
};
这也不起作用:
//B.h
class A;
enum A::type; //still use of undefined type
class B...
没问题,如果枚举是在Ah的全局范围内声明的
There's no problem if the enum is declared at global scope in A.h.
有没有办法做到这一点?
Is there any way to do this?
推荐答案
您不能在类定义之外声明嵌套类型。
You can't declare nested types outside the class definition.
如果您需要在类外使用它们,则可以要么包含类定义,要么将它们移到名称空间。
If you need to use them outside the class, you will have to either include the class definition, or move them into a namespace.
这篇关于C ++前向声明类范围的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!