Archived

This topic is now archived and is closed to further replies.

finding the offset of a variable in a class

This topic is 5405 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, first, why would you want to, and second... like this:


  
class Test_C
{
public:
int FirstVar;
int SecondVar;
int ThirdVar;
};

Test_C MyTest;
int OffsetOfThirdVar;

OffsetOfThirdVar = (&MyTest.ThirdVar-&MyTest);


This should give you the offset of ThirdVar, because you take the location of the ThirdVar, and subtract the base address of the class, giving you the difference (offset). This would be the easiest way to do it, and it will hold true even if you change your class around (it will always be correct, unlike if you just calculate it). I still have no clue why you''d want to do this, but whatever floats your boat I guess.

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
OffsetOfThirdVar = (&MyTest.ThirdVar-&MyTest);



Your pointer types don't match. This will not work.

quote:
Original post by daerid
look in <stddef.h>

offsetof() macro



That's a C macro, it may not work in C++ (vtbl anyone ?)

The 'right' way would be to use a pointer to member :
int Test_C::*ptr = &Test_C::SecondVar; 


It may not directly translate to an offset, but, when combined with an actual object, it resolves to the member.

Test_C obj;
int i = obj.*ptr;
int* pi = &obj.*ptr;

Test_C *pobj = &obj;
int j = pobj->*ptr;
int* pj = &pobj->*ptr;




[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on March 2, 2003 9:58:52 PM]

Share this post


Link to post
Share on other sites
quote:

That's a C macro, it may not work in C++ (vtbl anyone ?)


I dunno. I did use it in VC7.0 with a vtbl, and it did work, but I wouldn't ever want to rely on that bit of informatino.

Slightly off topic..
how do I declare an pointer to a member array ?

Wait...ahhah!
this is how you do it:
For the record:

float (some_class::*name_of_var)[];
cout << (some_instance.*name_of_var)[some_index];

I like answering my own questions!

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

EDIT:
You dont' actually have to have the size of the array
yay.

[edited by - risingdragon3 on March 2, 2003 10:51:25 PM]

Share this post


Link to post
Share on other sites
Ha, risingdragon, I was just about to tell you about this thread

Anyway, they''ve about said it all.




Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
i''m trying to edit variables in real time based on an std::map of strings, so i can keep the names of variables through to runtime. offsetof will work i think, i''m reading more about it to see if it works with the vtable

Share this post


Link to post
Share on other sites
What you are trying to do is a little of reflection...I''m doing the same thing too ...
also with functions and stuff ..it''s really cool

Right now I did the compile-time level stuff - under my code there is no overhead..
But once I do the runtime stuff, there is going to be overhead.
I''ve already implemented a ton of the stuff..Now to runtime!

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

Share this post


Link to post
Share on other sites
my system has it so using the variables is the same as a normal variable, no overhead other than memory, and editing them is not a normal thing, so i have nothing to compare to. i think the only overhead is in the std::map ''search'' for the corrosponding element.

i''m using mostly macros to group variables (structs), so in the property editor it shows up as a plus with the variables showing up when its expanded. you can also have pluses within pluses, its pretty cool.

how did you get the location of the variable in the class (the offset) ?

Share this post


Link to post
Share on other sites
I used member pointers. Except on VC7 they are broken when you access an member in a struct with one member (I know, I used GCC to check!)

I dunno, my code is a little complex. (hah!) I am going to use it for things like reflection into scripting engines and stuff, serialization, accessing things generically in a scene graph, stuff like that.
It''s kinda like a boost:roperty map on steroids

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

Share this post


Link to post
Share on other sites
thats what i''m after but only for real time property changing, and telling what variables are supposed to be transferred on the net reliably/unrealiably, or not at all, i plan on having each type edited differently. enums will be drop down menus (also macro''ed into the program), bools true/false toggle button, strings will be text enter boxes, even some advanced ones like colors maybe. 8 bit values will be a slider, it will be cool (if i can make it work )

i''m trying to avoid having to create an instance of the class and subtracting the pointers to the member and the class, i''m hoping offsetof(...) will do the trick.

Share this post


Link to post
Share on other sites