Re: a question about private access
From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 07/04/04
- Previous message: KPR: "I need more eyes on this one."
- In reply to: Martin Domig: "Re: a question about private access"
- Next in thread: jeffc: "Re: a question about private access"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: KPR: "I need more eyes on this one."
- In reply to: Martin Domig: "Re: a question about private access"
- Next in thread: jeffc: "Re: a question about private access"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|