嵌套枚举的前向声明 [英] Forward declaration of nested enum
本文介绍了嵌套枚举的前向声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码类似于以下内容:
class B
{
}
class A
{
enum {
EOne,
ETwo
} EMyEnum;
B myB;
}
我想在类B中声明一个类型为EMyEnum的成员(已声明之前A)。这可能吗?我意识到解决方案是声明B类第二,但为了清楚,我不愿意。
解决方案
这是不可能的但是它可以被伪造成继承性滥用:)
命名空间详细信息
{
class A_EMyEnum
{
public:
enum {
EOne,
ETwo
} EMyEnum;
protected:
A_EMyEnum(){}
A_EMyEnum(const A_EMyEnum&){}
A_EMyEnum& operator =(const A_EMyEnum&){return * this; }
〜A_EMyEnum(){}
}; // class A_EMyEnum
} // namespace detail
class B {// use detail :: A_EMyEnum};
class A:public detail :: A_EMyEnum
{
B mB;
};
另一方面...为什么不转发声明B?
class B;
class A
{
public:
枚举EMyEnum {};
A();
A(const A&);
A& operator =(const A&);
〜A();
void swap(A&);
private:
B * mB;
};
class B {//使用A :: EMyEnum};确定您需要实际写入A的所有正常的默认生成方法,但是嘿,那么不花费太多!
I have code similar to the following:
class B
{
}
class A
{
enum {
EOne,
ETwo
} EMyEnum;
B myB;
}
I want to declare a member of type EMyEnum in class B (which is declared before A). Is this possible? I realise the solution is to declare class B second, but for clarity I would prefer not to.
解决方案 It's not possible... but it can be faked with inheritance abuse :)
namespace detail
{
class A_EMyEnum
{
public:
enum {
EOne,
ETwo
} EMyEnum;
protected:
A_EMyEnum() {}
A_EMyEnum(const A_EMyEnum&) {}
A_EMyEnum& operator=(const A_EMyEnum&) { return *this; }
~A_EMyEnum() {}
}; // class A_EMyEnum
} // namespace detail
class B { // use detail::A_EMyEnum };
class A: public detail::A_EMyEnum
{
B mB;
};
On the other hand... why don't you simply forward declare B ?
class B;
class A
{
public:
enum EMyEnum {};
A();
A(const A&);
A& operator=(const A&);
~A();
void swap(A&);
private:
B* mB;
};
class B { // use A::EMyEnum };
Sure you need to actually write all the normally "default generated" methods of A, but hey that does not cost so much!
这篇关于嵌套枚举的前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文