Jump to content
  • Advertisement

Archived

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

billybob

finding the offset of a variable in a class

This topic is 5620 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

Advertisement
Guest Anonymous Poster
Don''t. Nevertheless. Why?

Share this post


Link to post
Share on other sites
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!