作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有类似下面的代码:
class B
{
}
class A
{
enum {
EOne,
ETwo
} EMyEnum;
B myB;
}
我想在 B 类中声明一个 EMyEnum 类型的成员(在 A 之前声明)。这可能吗?我意识到解决方案是先声明 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 };
当然,您需要实际编写 A 的所有通常“默认生成”的方法,但是,这并不需要花费这么多!
关于c++ - 嵌套枚举的前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2238170/
我是一名优秀的程序员,十分优秀!