access private member function

Discussion in 'C++' started by Aakash Joshi, Aug 24, 2004.

  1. Aakash Joshi

    Aakash Joshi Guest

    is there any way we can access private member function of
    a class from outside.

    i know we can access private member data using void pointer
     
    Aakash Joshi, Aug 24, 2004
    #1
    1. Advertising

  2. Aakash Joshi wrote:
    > is there any way we can access private member function of
    > a class from outside.


    Only if the function from which you're trying to access the
    private member is declared a friend or is a member of a class
    that is declared a friend.

    > i know we can access private member data using void pointer


    Huh?
     
    Victor Bazarov, Aug 24, 2004
    #2
    1. Advertising

  3. Aakash Joshi

    tom_usenet Guest

    On 24 Aug 2004 05:44:09 -0700, (Aakash Joshi)
    wrote:

    >is there any way we can access private member function of
    >a class from outside.
    >
    >i know we can access private member data using void pointer


    The best way to do it is to make the class you want to access the
    function a friend - note that you can do this even if the class in
    question is in a library. Adding a friend declaration to a class
    doesn't require it to be recompiled.

    If you can't modify the header for some reason, then the usual
    technique is to add:

    #define private public

    before including the header. Obviously this doesn't work if the
    function is at the top of the class, before any private:.

    Tom
     
    tom_usenet, Aug 24, 2004
    #3
  4. tom_usenet wrote:

    > On 24 Aug 2004 05:44:09 -0700, (Aakash Joshi)
    > wrote:
    >
    >
    >>is there any way we can access private member function of
    >>a class from outside.
    >>
    >>i know we can access private member data using void pointer

    >
    >
    > The best way to do it is to make the class you want to access the
    > function a friend - note that you can do this even if the class in
    > question is in a library. Adding a friend declaration to a class
    > doesn't require it to be recompiled.

    AFAIK, that is undefined

    > If you can't modify the header for some reason, then the usual
    > technique is to add:
    >
    > #define private public
    >
    > before including the header. Obviously this doesn't work if the
    > function is at the top of the class, before any private:.

    ehm.. ever heard of the ONE-definition rule?

    If you don't have the source of a library, don't change the headers that
    come with it, and don't try to get access to members the authors didn't
    want you to have access to: you might be breaking invariants without
    knowing it.

    If you do have access to the sources, make sure you don't break anything
    by accessing the private members you're accessing and/or add accessors
    and mutators to the class that keep the invariants in tact - consult
    with the original author if you can and document your interpretation of
    the class' invariants in case you break any you hadn't found/didn't know
    about.

    rlc
     
    Ronald Landheer-Cieslak, Aug 24, 2004
    #4
  5. "tom_usenet" <> skrev i en meddelelse
    news:...
    > On 24 Aug 2004 05:44:09 -0700, (Aakash Joshi)
    > wrote:
    >
    > >is there any way we can access private member function of
    > >a class from outside.
    > >
    > >i know we can access private member data using void pointer

    >
    > The best way to do it is to make the class you want to access the
    > function a friend - note that you can do this even if the class in
    > question is in a library. Adding a friend declaration to a class
    > doesn't require it to be recompiled.
    >
    > If you can't modify the header for some reason, then the usual
    > technique is to add:
    >
    > #define private public
    >
    > before including the header. Obviously this doesn't work if the
    > function is at the top of the class, before any private:.
    >
    > Tom


    And not portable, so far as i know? Isn't the compiler allowed to rearrange
    members at a protection-boundary?

    /Peter
     
    Peter Koch Larsen, Aug 24, 2004
    #5
  6. Aakash Joshi

    tom_usenet Guest

    On Tue, 24 Aug 2004 20:54:30 +0200, "Peter Koch Larsen"
    <> wrote:

    >
    >"tom_usenet" <> skrev i en meddelelse
    >news:...
    >> On 24 Aug 2004 05:44:09 -0700, (Aakash Joshi)
    >> wrote:
    >>
    >> >is there any way we can access private member function of
    >> >a class from outside.
    >> >
    >> >i know we can access private member data using void pointer

    >>
    >> The best way to do it is to make the class you want to access the
    >> function a friend - note that you can do this even if the class in
    >> question is in a library. Adding a friend declaration to a class
    >> doesn't require it to be recompiled.
    >>
    >> If you can't modify the header for some reason, then the usual
    >> technique is to add:
    >>
    >> #define private public
    >>
    >> before including the header. Obviously this doesn't work if the
    >> function is at the top of the class, before any private:.
    >>
    >> Tom

    >
    >And not portable, so far as i know? Isn't the compiler allowed to rearrange
    >members at a protection-boundary?


    Of course, but I don't know of any that actually do. So I think it's
    portable, just not standard, if you see the distinction.

    Tom
     
    tom_usenet, Aug 25, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy
    Replies:
    5
    Views:
    899
    JustSomeGuy
    Dec 5, 2004
  2. Mike
    Replies:
    9
    Views:
    367
  3. Peng Yu
    Replies:
    3
    Views:
    1,085
    Simon Forman
    Sep 21, 2009
  4. ittium
    Replies:
    5
    Views:
    445
    88888 Dihedral
    Jan 12, 2012
  5. Gregor Kofler
    Replies:
    6
    Views:
    213
    Gregor Kofler
    Jun 27, 2008
Loading...

Share This Page