finding the offset of a variable in a class

Started by
10 comments, last by billybob 21 years, 1 month ago
how do i find the offset of a variable in a class?
Advertisement
Don''t. Nevertheless. Why?
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.
look in <stddef.h>

offsetof() macro
daerid@gmail.com
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 = &objint 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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]
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.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
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
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.
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) ?

This topic is closed to new replies.

Advertisement