Access to method pointer to protected method?

前端 未结 6 632
情歌与酒
情歌与酒 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:02

    Your post doesn't answer "Why can I call a protected method but not take its address?"

    class D : public B {
     public:
      void Baz() {
        // this line
        Foo();
        // is shorthand for:
        this->Foo();
      }
      void Bar() {
        // this line isn't, it's taking the address of B::Foo
        printf("%x\n", &B::Foo);
    
        // not D:Foo, which would work
        printf("%x\n", &D::Foo);
    
      }
    }
    

提交回复
热议问题