c++ - How to access an attribute into a struct from a void pointer? -


i given task can't see function in detail know id generally.

so have function, let's call "get_button", , in documentation know "it returns pointer button struct". know button struct, it's like:

struct button{     int size;     char title;     short keyid; }; 

the function like:

void * get_button(int id, int group) 

but can't know does.

now want know title, playing complier got have void pointer in return. how supposed use void pointer? asaik have use static_cast give reference it, in case struct; can't understand casting means , how make compiler know want struct.

this particular case need know how parameter struct void pointer. don't know if should use static_cast or whatever (so can't understand reference it). neither need know how use propery void pointer in case! need know how can access attribute in struct. i've seen other answers here , there couldn't find valuable case.

i can't understand casting means

you answer in next question:

how make compiler know want struct.

that's it. casting means tell compiler, object has specific type.

this particular case need know how parameter struct void pointer.

first, cast void pointer pointer type of object points to: *button. can access attribute normally, using operator-> on pointer of correct type.

i don't know if should use static_cast or whatever

when casting void pointer pointer type, should indeed use static_cast.

void* void_pointer = get_button(id, group); // pointer button* button_pointer = static_cast<button*>(void_pointer); // cast type of pointed object int size = button_pointer->size; // access attribute through pointer 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -