Sign in to follow this  

casting from void * to dynamic object

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

I've come across the need to dereference a void * pointer by casting it into the object it's supposed to be (using c++). The problem is that the object is one defined by me that contains dynamic members such as STL containers that change in size during runtime. An example is:
typedef struct SMALL
{
int m1;
char c2;
void * _parent;
} SMALL;

typedef struct BIG
{
int a;
int b;
vetor<SMALL> pieces;
} BIG;

Thus the BIG object (which could also be a class, as I'm thinking I have to do anyway) contains a dynamic array of SMALL objects. The SMALL objects need to be able to reference their respective parents (which will always be BIG objects) when called to do so. Thus at some point all the SMALLs of the 'pieces' member of a BIG object are set to point to the BIG object as their parent. Later, to retrieve the parent of any given SMALL object (obtained through some means) you would normally do something like:
//'current'  is a SMALL object that is given
BIG * thisone = (BIG *)current._parent;
//get info from the parent object
cout<<"This is member a: "<<thisone->a<<endl;



This only seems to work if the object that the now valid parent pointer is casting to is an object of static size, which makes sense I suppose. I've thought of dynamic_cast, but that doesn't work on void *. I admit I don't quite know exactly what's going on during casting like this. How can this be done? I've thought of making the BIG object into a class and defining a function that will retrieve information from a void * pointer passed to it but I don't know how to figure out what part of the pointer is what, which I guess is the compiler's problem too. Sorry about the length of this. Any suggestions?

Share this post


Link to post
Share on other sites
Define an interface with virtual functions, derive BIG from that. Now small can call the virtual functions without a cast.

Also, instead of each small holding a big pointer, if BIG always calls small, you can just pass the "this" from the big into the small function when you call it. If you have 1 million smalls in your various bigs, you've just saved yourself 4MB of RAM. If smalls can be called by other things which may not know about BIG then you don't really get that option.

Share this post


Link to post
Share on other sites
Quote:
Original post by Syreth
I've come across the need to dereference a void * pointer by casting it into the object it's supposed to be (using c++). The problem is that the object is one defined by me that contains dynamic members such as STL containers that change in size during runtime. An example is:

*** Source Snippet Removed ***

Thus the BIG object (which could also be a class, as I'm thinking I have to do anyway) contains a dynamic array of SMALL objects. The SMALL objects need to be able to reference their respective parents (which will always be BIG objects) when called to do so.


You don't need pointers to void for this.

Quote:
Original post by Syreth
I've thought of dynamic_cast, but that doesn't work on void *.


dynamic_cast is for casting between related types in a type hierarchies, it nagivates type hierarchies.

Quote:
Original post by Syreth
How can this be done?



#include <vector>

struct big;

struct small {
int m1;
char c2;
big* _parent;
};

struct big {
int a;
int b;
std::vetor<small> pieces;
};




Quote:
Original post by Syreth
I've thought of making the BIG object into a class


There is no difference betweewn structures & classes except the access & inheritance structures default to public while classes default to private.

Also typedef'ing structs on declaration of user-defined type is completely redundant in c++, its done in C because if you don't every time you use it you would have to type struct before the declaration of a variable.

Share this post


Link to post
Share on other sites

This topic is 4814 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this