Calling private method in C++

后端 未结 12 2205
忘了有多久
忘了有多久 2020-11-27 20:02

This is purely a theoretical question, I know that if someone declares a method private, you probably shouldn\'t call it. I managed to call private virtual methods and chang

12条回答
  •  长情又很酷
    2020-11-27 20:52

    See my blog post. I'm reposting the code here

    template
    struct result {
      /* export it ... */
      typedef typename Tag::type type;
      static type ptr;
    };
    
    template
    typename result::type result::ptr;
    
    template
    struct rob : result {
      /* fill it ... */
      struct filler {
        filler() { result::ptr = p; }
      };
      static filler filler_obj;
    };
    
    template
    typename rob::filler rob::filler_obj;
    

    Some class with private members

    struct A {
    private:
      void f() {
        std::cout << "proof!" << std::endl;
      }
    };
    

    And how to access them

    struct Af { typedef void(A::*type)(); };
    template class rob;
    
    int main() {
      A a;
      (a.*result::ptr)();
    }
    

提交回复
热议问题