所以我使用Visual Studio 2008进行了以下安排:
class Base {
protected:
int data_;
protected:
enum {special_sauce = 1234};
};
class Derived : public Base {
friend class DataAccessor;
};
class DataAccessor {
public:
int AccessData(Derived* object){
return object->data_;
}
bool CheckData(Derived* object){
return object->data_ == Derived::special_sauce;
}
};
一切运作良好。现在,进行此更改(使基础和派生(编辑:替换"子类") DataAccessor的内部类...)
class DataAccessor {
public:
class Base {
protected:
int data_;
protected:
enum {special_sauce = 1234};
};
class Derived : public Base {
friend class DataAccessor;
};
public:
int AccessData(Derived* object){
return object->data_;
}
bool CheckData(Derived* object){
return object->data_ == Derived::special_sauce;
}
};
我收到以下错误:
error C2248: 'special_sauce' : cannot access protected enumerator declared in class 'DataAccessor::Base'
see declaration of 'special_sauce'
see declaration of 'DataAccessor::Base'
为什么会发生这种情况,为什么会员data_
不会以同样的方式受到影响?
答案 0 :(得分:2)
对于VS编译器,听起来类似于this bug report。更多讨论here。
class BaseClass{
public :
friend class FriendOfBaseClass;
protected :
static void f() {};
class InnerBaseClass {};
enum TheType { state_0=0 };
};
class FriendOfBaseClass {};
class Class2 {
public :
class InnerClassOfClass2 : public BaseClass {
public :
friend class FrienOfInnerClass;
};
};
class FrienOfInnerClass : public FriendOfBaseClass {
public :
void TheProblemIsHere()
{
Class2::InnerClassOfClass2::f(); // This compiles well
Class2::InnerClassOfClass2::InnerBaseClass y; // This compiles well
int x=Class2::InnerClassOfClass2::state_0; // Compilation error here!
}
};
错误C2248:'state_0':无法访问类'BaseClass'中声明的受保护枚举器