-
19-09-2019 - |
题
我具有类似于下面的代码:
class B
{
}
class A
{
enum {
EOne,
ETwo
} EMyEnum;
B myB;
}
我想声明类型EMyEnum类B(其是前声明)的成员。这可能吗?我实现的解决方案是声明类B秒,但为了清楚起见,我宁愿不
解决方案
这是不可能的...但它可以继承滥用伪造:)
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;
};
在另一方面......你为什么不干脆向前声明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 };
当然,你需要实际编写所有的通常是“默认生成”一个方法,但嘿不费那么多了!
其他提示
在当前的C ++标准不允许向前enum
s的声明,尽管它们将在即将到来的C ++ 0x标准到来。
请参阅此处的详细信息。
可以声明B.的作为模板的参数 第二方法来解决它使用INT - 。已知的是,C ++枚举为int
不隶属于 StackOverflow