Board index » cppbuilder » Problem accessing public members of a class by another class

Problem accessing public members of a class by another class


2006-01-23 07:24:08 AM
cppbuilder42
Using BDS 2006,
This is what I THINK the essence of the problem:
I have two classes, call them A and B
class B
public:
int x;
In the constructor of class B, x is assigned a value of 5
Additionally I also have following function in class B:
int GetX(void) which returns the value of x
class A
public:
B MyB ;
In the constructor of class A I attempt to display the content of x (in
class B)
Caption = String(MyB.x) + String (" - ") + String(MyB.GetX()) ;
This always produces a result which doesn't make sense:
0 - 5
In other words only the function returns the correct value, directly
accessing the member doesn't work.
I traced back the problem to following section in one of the header files
(the project is obviously more complex):
#include <pshpack4.h>
typedef struct
{
DWORD dwAccessType;
LPWSTR lpszProxy;
LPWSTR lpszProxyBypass;
} WINHTTP_PROXY_INFO;
#include <poppack.h>
If I change the byte packing to the following :
#pragma option -a1
typedef struct
{
DWORD dwAccessType;
LPWSTR lpszProxy;
LPWSTR lpszProxyBypass;
} WINHTTP_PROXY_INFO;
#pragma option -a.
suddenly everything works great and I get 5 - 5
Note, when the problem occurs, the erroneous value (0) appears to be the
value of another member of class B (in fact the preceding member in memory).
Because wen I init (during construction) that preceding member's value to
something else than 0 then that value is returned on MyB.x
So looks like the byte packing (see example) is messing up something in
memory and causes the code (which SEEMS ok) to fail ??
Does anybody have some idea on what is going on ?
Is it possible that <pshpack4.h>and <poppack.h>cause problems with BDS ?
I never saw this with BCB6 ? (yet current code has changed considerably
since then)
 
 

Re:Problem accessing public members of a class by another class

Peter wrote:
Quote
I have two classes, call them A and B
Please take the time to make minimal code that actually compiles and links
and behaves like you described. Post it here so we can see what happens.
Quote
So looks like the byte packing (see example) is messing up something in
memory and causes the code (which SEEMS ok) to fail ??

Does anybody have some idea on what is going on ?
Byte packing indeed. For the rest post code.
Hans.
 

Re:Problem accessing public members of a class by another class

I posted this question for a colleague who was travelling and would follow
up immediately on return but he's currently not feeling well and will pick
up this thread shortly.
Thanks for your patience.
 

{smallsort}

Re:Problem accessing public members of a class by another class

Actually, in my big project (which I cannot publically post) the
problem was abudantly clear and could be fixed by changing the
#include <pshpack4.h>/<poppack.h>into a #pragma option -a1/.
However, my attempts to recreate this behaviour is a small code sample
have failed. So, unfortunatetly its not possible for me to post any
code that reproduces this problem.