C ++错误:基本功能受保护 [英] C++ error: base function is protected

查看:269
本文介绍了C ++错误:基本功能受保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么下面的代码不能编译:

  class base {
protected:
typedef void(base :: * function_type)()const;
void function_impl()const {} //错误:'void base :: function_impl()const'受保护
};

class derived:public base {
public:
operator function_type()const {
return boolean_test()== true? & base :: function_impl:0; //错误:在这种情况下
}

保护:
虚拟布尔boolean_test()const = 0;
virtual〜derived(){}
};

int main(int argc,char * argv []){
}

g ++ 输出:

 〜/ protected_test $ g ++ src / protected_test.cpp 
src / protected_test.cpp:在成员函数'derived :: operator base :: function_type()const'中:
src / protected_test.cpp:4:8:error:'void base :: function_impl()const'受到保护
src / protected_test.cpp:10:44:error:在这种情况下

这段代码是从这里改编而来的,我看不到有人抱怨讨论论坛。此外,我正在使用g ++ 4.7.2,并且使用相同的代码编译并链接到egcs-2.91.66。

解决方案

受保护访问的规范规定,指向成员的指针必须通过派生类型(即 derived :: ... )或从它继承的类型来形成。您不能通过 base 直接命名 function_impl



这意味着,在你的情况下,你必须做它作为

  operator function_type()const {
return boolean_test )== true? & derived :: function_impl:0;

$ / code>

请注意,即使您使用& derived: :function_impl 表达式来获取地址,结果的类型仍然是 void(base :: * function_type)()const ,因为名字 function_impl 在这种情况下解析为 base 类的功能。



如果它用于在某个特定的编译器(或其特定版本)中进行编译,它仅仅意味着该编译器允许错误通过,这可能是链接中代码的解释。


I would like to know why the following code does not compile:

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++ output:

~/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

This code was adapted from here and I see no one complaining about that at the discussion forum. Also, I'm using g++ 4.7.2 and the same code compiles and links fine with egcs-2.91.66.

解决方案

The specification of protected access states that pointer to members have to be formed either through the derived type (i.e. derived::...) or a type inherited from it. You can't name function_impl directly through base.

That means that in your case you have to do it as

operator function_type() const {
  return boolean_test() == true ? &derived::function_impl : 0;
}

Note that even if you use &derived::function_impl expression to obtain the address, the type of the result is still void (base::*function_type)() const, since the name function_impl in this case resolves to the function of base class.

If it used to compile in some specific compiler (or some specific version of it), it simply means that that compiler allowed the error to slip through, which is what probably explains the code at the link.

这篇关于C ++错误:基本功能受保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆