October 6th, 2003, 09:52 PM
control access to structure data in C
yes i know C++ is made for just this purpose, encapsulating data, but lets say i was one of those people who does things the hard way, so:
is there any possible hideously ugly way (uglier == better) to do this. i thought there i would be able to define a macro function like [x], but they have to be alphanumeric. lets say i have this structure with a member, now whenever someone tries to access it like,
i want to bounds check first against another member(s) of the structure. i could use a regular function, but i'd rather be able to doit simply, like:
packet_q[x]; or packet_q[x][y];
or something similar. is that possible? if not with macros, then some sort of preprocessor code that will expand to what i want? the main problem i see is the lack of a *this pointer for structures. the max value i want to check against is a member of the same structure as packet_q. so i was thinking if i can get the address of the packet_q i could figure out it's offset in relation to the max value i need. does that make sense?
for now i'm doing this:
but this means i need a #define or global variable, max, or worse adding another variable to pass to the macro.
#define a(x) ( (x <= max) ? x : max )
October 7th, 2003, 06:58 AM
Are you trying to get a prize for writting obscure code or something?
October 7th, 2003, 08:06 AM
I know that the  are operators. You might check if you can overload them, but I don't think so. If not, anyone who uses that member is still going to use some other operator with it-- you can overload most of the rest of the operators to do the bounds checking.
October 7th, 2003, 12:35 PM
lol i think so :D i get these strange ideas in my head and then i get determined to make them work. i was toying with overloading  in C++, but g++ won't compile unless  pertains to a certain class. ah well at least i tried.