【发布时间】:2013-06-06 19:56:03
【问题描述】:
我想知道为什么下面的代码不能编译:
class base {
protected:
typedef void (base::*function_type)() const;
void function_impl() const {} // error: ‘void base::function_impl() const’ is protected
};
class derived: public base {
public:
operator function_type() const {
return boolean_test() == true ? &base::function_impl : 0; // error: within this context
}
protected:
virtual bool boolean_test() const = 0;
virtual ~derived() {}
};
int main(int argc, char* argv[]) {
}
g++ 输出:
~/protected_test$ g++ src/protected_test.cpp
src/protected_test.cpp: In member function ‘derived::operator base::function_type() const’:
src/protected_test.cpp:4:8: error: ‘void base::function_impl() const’ is protected
src/protected_test.cpp:10:44: error: within this context
这段代码改编自here,我在论坛上看到没有人抱怨这一点。另外,我使用的是 g++ 4.7.2,并且相同的代码可以与 egcs-2.91.66 进行编译和链接。
【问题讨论】:
-
要么一个编译器有问题,要么另一个。 :D