我有一个必须在其他类 Bar 中“直接”访问的类 Foo。我想构建一个小框架,声明 Bar 的方法(它是 Foo 的友元方法)受保护。通过这种方式,我可以构建几个 Bar 的子类。
Gcc 对此抱怨,并且仅当该方法是公开的时它才有效。
我能怎么做?我的代码示例:
class Foo;
class Bar {
protected:
float* internal(Foo& f);
};
class Foo {
private:
//some data
public:
//some methods
friend float* Bar::internal(Foo& f);
};
海合会消息:
prog.cpp:4:16: error: ‘float* Bar::internal(Foo&)’ is protected
float* internal(Foo& f);
^
prog.cpp:11:43: error: within this context
friend float* Bar::internal(Foo& f);
^