accessing a private member (illegally)

Discussion in 'C++' started by Denis Remezov, Aug 17, 2004.

  1. Phlip wrote:
    >
    > Tom wrote:
    >
    > > Let's say I've got a class defined in a header file.
    > >
    > > class foo {
    > > private:
    > > ...
    > > int bar;
    > > ....
    > > };
    > >

    [...]
    >
    > > 3) access bar through low-level pointer hacks. This is possible, but
    > > compiler dependant and non portable.

    >
    > They are portable when they use offsetof(). It portably returns the offset
    > of a member; you typecast the address of an object to
    >


    Since offsetof is only defined for PODs, it cannot be used legally (or
    portably for that matter, if only because many compilers would bulk at such
    attempt) for any private or protected data members.

    Denis
    Denis Remezov, Aug 17, 2004
    #1
    1. Advertising

  2. Denis Remezov

    Tom Guest

    Let's say I've got a class defined in a header file.

    class foo {
    private:
    ...
    int bar;
    ....
    };

    Now lets say I have a function that needs to access the private variable
    bar:

    void function() {
    ....
    foo x;
    x.bar = 5; // illegal, bar is private
    }

    Of course, the normal way to do this is with a friend declaration. But
    lets say I have a constraint where I cannot modify the header file.
    Basically, I don't want to change anything inside the class foo {...}
    declaration. Is there any other way to get access to x.bar from
    function()?

    Things that come to mind:

    1) make the friend declaration outside the header file. It would be
    nice if something like this worked:
    foo::friend void main();
    But I can't find any syntax that accomplishes this, so I'm thinking it
    isn't possible.

    2) "cast" the member variable to remove it's private modifier. I don't
    think C++ allows this either.

    3) access bar through low-level pointer hacks. This is possible, but
    compiler dependant and non portable.

    Are there any other options? I've got my shotgun loaded and pointing in
    the vicinity of my foot, now I just need to figure out how to remove the
    safety lock. :)
    Tom, Aug 17, 2004
    #2
    1. Advertising

  3. Denis Remezov

    Phlip Guest

    Tom wrote:

    > Let's say I've got a class defined in a header file.
    >
    > class foo {
    > private:
    > ...
    > int bar;
    > ....
    > };
    >
    > Now lets say I have a function that needs to access the private variable
    > bar:
    >
    > void function() {
    > ...
    > foo x;
    > x.bar = 5; // illegal, bar is private
    > }
    >
    > Of course, the normal way to do this is with a friend declaration. But
    > lets say I have a constraint where I cannot modify the header file.
    > Basically, I don't want to change anything inside the class foo {...}
    > declaration. Is there any other way to get access to x.bar from
    > function()?
    >
    > Things that come to mind:
    >
    > 1) make the friend declaration outside the header file. It would be
    > nice if something like this worked:
    > foo::friend void main();
    > But I can't find any syntax that accomplishes this, so I'm thinking it
    > isn't possible.


    It's not possible because it defeats the point of privacy and friendship.
    When I inspect the header file X.h, I only need to read the file to know
    what's private, what's restricted, and what's public.

    > 2) "cast" the member variable to remove it's private modifier. I don't
    > think C++ allows this either.


    Privacy is not a type or qualification, it is an access.

    > 3) access bar through low-level pointer hacks. This is possible, but
    > compiler dependant and non portable.


    They are portable when they use offsetof(). It portably returns the offset
    of a member; you typecast the address of an object to

    > Are there any other options? I've got my shotgun loaded and pointing in
    > the vicinity of my foot, now I just need to figure out how to remove the
    > safety lock. :)


    The other option is to modify that header file, or use the interface of its
    constituents the way they were designed to be used.

    You could also write this above the header:

    #define private public

    The C++ Standard undefines the act of re-defining a keyword above the
    #include for a Standard header file. It also undefines the result of linking
    two translation units that disagree on the exact definition of a class they
    share (one definition rule). But nobody checks these violations...

    However, in this case, the bullet /might/ go thru your foot with only minor
    soft-tissue damage, so try it!

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Aug 17, 2004
    #3
  4. Denis Remezov

    Tom Guest

    "Phlip" <> wrote in
    news:qcgUc.1752$:

    BTW, before I go on, I should mention that the application underlying
    this question is an object serialization framework I am developing --
    one of the canonical examples of low-level functionality when access
    qualifiers just get in the way.

    > They are portable when they use offsetof(). It portably returns the
    > offset of a member; you typecast the address of an object to


    Good idea, but it sadly it doesn't work. At least not with g++. I
    still get an error that the member variable is private. Looking at the
    definition in stddef.h I'm not surprised. You can't take an address of
    a private member.

    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

    > The other option is to modify that header file


    Not an option for two reasons. It's a general framework, it would be
    prohibitive to modify every header file its ever applied to. Also, it
    could break binary code compiled with the original headers, as you noted
    below, unless the change has absolutely no effect on the binary
    structure.

    > You could also write this above the header:
    >
    > #define private public


    Now this is promising. :) In fact I could use a slight variation:
    #define private friend void function(); private

    This almost works, but it will fail if the class defines private
    variable implicitly at the top of the class without ever using the
    private keyword. But maybe that could be fixed with:
    #define class struct. (hehe...)
    I would also need to verify that this change doesn't change the binary
    structure on compilers I care about.

    > However, in this case, the bullet /might/ go thru your foot with only
    > minor soft-tissue damage, so try it!


    Thanks for your suggestions, I'm getting closer. I'm going to try
    experimenting with the preprocessor hacks and testing if it's good
    enough to get the job done.

    It would be great if the addressof operator worked, because that would
    be a perfect solution.
    Tom, Aug 17, 2004
    #4
  5. Denis Remezov

    Old Wolf Guest

    Tom <> wrote:

    > void function() {
    > ...
    > foo x;
    > x.bar = 5; // illegal, bar is private
    > }
    >
    > Of course, the normal way to do this is with a friend declaration. But
    > lets say I have a constraint where I cannot modify the header file.
    > Basically, I don't want to change anything inside the class foo {...}
    > declaration. Is there any other way to get access to x.bar from
    > function()?


    You may find this useful: http://www.gotw.ca/gotw/076.htm
    Old Wolf, Aug 18, 2004
    #5
    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. Shiperton Henethe
    Replies:
    1
    Views:
    819
    tornell
    Jul 5, 2003
  2. Shiperton Henethe
    Replies:
    1
    Views:
    943
    Shiperton Henethe
    Jun 25, 2003
  3. Andy
    Replies:
    1
    Views:
    399
    Brian Larmay
    Jul 5, 2003
  4. Andy
    Replies:
    1
    Views:
    412
    Toby A Inkster
    Jul 6, 2003
  5. Peng Yu
    Replies:
    3
    Views:
    1,068
    Simon Forman
    Sep 21, 2009
Loading...

Share This Page