相关疑难解决方法(0)

C++错误:基本功能受到保护

我想知道为什么以下代码无法编译:

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[]) {
}
Run Code Online (Sandbox Code Playgroud)

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’ …
Run Code Online (Sandbox Code Playgroud)

c++ g++

9
推荐指数
1
解决办法
1762
查看次数

标签 统计

c++ ×1

g++ ×1