c ++如何为同一成员创建public和受保护的访问者

c++ how to create public and protected accessors to same member

如果我有两种方法 - 一种 public,一种保护 return 对同一成员的引用,我会得到以下编译错误:

'Server::getManager': cannot access protected member declared in class 'Server'

当我注释掉受保护的函数时,代码可以正常工作。你能告诉我为什么会这样吗?为什么编译器找不到相同成员的 public 函数?

class Manager
    {
    };

class Server
{
public:
    const Manager & getManager() const { return m_man; }
protected:
    Manager & getManager() { return m_man;  } // <-- after removing this method I get no compilation error

private:

    Manager m_man;
};

int main()
{

    Server s;
    const Manager& m = s.getManager();
    return 0;
}

Why the compiler cannot find the public function to same member?

这不是问题所在。编译器找到 both 函数并执行重载决策以确定哪个是最佳可行候选者。两位候选人是:

Manager&       getManager()        // protected
Manager const& getManager() const  // public

对于成员函数,有一个隐含的第一个对象参数,它是 class 本身的实例。在这种情况下,两个函数变为:

getManager(Server& )         // protected
getManager(Server const& )   // public

我们在不是 const 的对象 (s) 上调用它。两个候选人都是可行的,但是 public 候选人比 protected 引用了 更多 cv 合格对象candidate - 所以它不太受欢迎。标准语在 [over.ics.rank]:

Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if
— S1 and S2 are reference bindings (8.5.3), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers.

因此,protected 候选人是首选 - 所以这就是所谓的候选人。

不幸的是,它是 protected,所以调用它是错误的。重载决议后检查访问控制。所以你必须以某种方式重组你的程序。您可以简单地将 s 转换为 const:

const Manager& m = const_cast<Server const&>(s).getManager();

这将使 protected 候选人无法生存。