Re: a question about private access

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 07/04/04

  • Next message: Pedro Graca: "Re: I need more eyes on this one."
    Date: Sun, 4 Jul 2004 14:03:41 +0100
    
    

    In article <pan.2004.07.04.12.00.58.964899@domig.net>, Martin Domig
    <martin@domig.net> writes
    > /* Access private members of a class. You have to know the exact
    > * layout of that class to do this, because you need to define a
    > * dummy class with the same layout.
    > * Again, the key to success is an old C typecast:
    > */
    > class foohack {
    > public:
    > int a, b, _state, c, d; // those are private in the original class
    > int e, f, g; // those are public in the original class
    > };

    As all the variables are ints that will likely work. However the
    compiler has a lot of room for modifying data layout if the members are
    under separate access specifiers (even just an explicit re-iteration of
    the one currently in force). Where the data members are of different
    types with different sizes and/or alignment requirements compilers may
    be taking advantage of that. For example:

    struct x {
       int i;
       char c;
    // rest of a public interface
    private:
       int j;
       char k;
    };

    can, I believe, be laid out in memory as:

    i, j, c, k

    whereas:

    struct y {
       int i;
       char c;
       int j;
       char k;
    };

    Must be laid out as:
     i, c, j, k;

    That may involve, for example, four more bytes of storage.

    Just because you can get source code to compile free of diagnostics does
    not mean that the C++ Standard in any way guarantees the result will
    work let alone work as expected.

    IIUC the original problem concerned a C++ acceptable class definition
    for a Delphi object. That means that all bets about layout are off. Any
    solution will be highly specific to using BCB as the compiler. IOWs we
    are dealing with a vendor specific extension to C++.

    -- 
    Francis Glassborow      ACCU
    Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
    For project ideas and contributions: http://www.spellen.org/youcandoit/projects
    

  • Next message: Pedro Graca: "Re: I need more eyes on this one."

    Relevant Pages

    • Re: Replacing fgets
      ... Even if u_int8_t is a typedef for unsigned char, ... Didn't your compiler complain here. ... offset is changed ... offset needs to be an int. ...
      (comp.lang.c)
    • Re: How to use a C++ class in .NET
      ... > absolutely compiler dependant. ... > public ref class MyClass ... > int funtion1(unsigned char* inBuffer, unsigned inType, unsigned char* ...
      (microsoft.public.dotnet.framework)
    • Re: How to use a C++ class in .NET
      ... absolutely compiler dependant. ... public ref class MyClass ... int funtion1(unsigned char* inBuffer, unsigned inType, unsigned char* ...
      (microsoft.public.dotnet.framework)
    • Re: querry related to structure padding
      ... char B; ... The compiler is still free to insert padding between B and C, ...
      (comp.lang.c)
    • Re: Error in Passing char pointer
      ... int main ... Always ensure that the compiler sees a prototype for each function before calling it. ... Why is ea unsigned char when everything else is signed char? ... Also your pointer types disagree with the pointers you are passing, unsigned char* and char* are not the same. ...
      (comp.lang.c)