Access to method pointer to protected method?

前端 未结 6 639
情歌与酒
情歌与酒 2020-12-03 17:27

This code:

class B {
 protected:
  void Foo(){}
}

class D : public B {
 public:
  void Baz() {
    Foo();
  }
  void Bar() {
    printf(\"%x\\n\", &B::F         


        
6条回答
  •  难免孤独
    2020-12-03 18:23

    I believe protected doesn't work the way you think it does in C++. In C++ protected only allows access to parent members of its own instance NOT arbitrary instances of the parent class. As noted in other answers, taking the address of a parent function would violate this.

    If you want access to arbitrary instances of a parent, you could have the parent class friend the child, or make the parent method public. There's no way to change the meaning of protected to do what you want it to do within a C++ program.

    But what are you really trying to do here? Maybe we can solve that problem for you.

提交回复
热议问题