Jump to content
  • Advertisement
Sign in to follow this  
BennettSteele

C++ Void* Questions

This topic is 2464 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 was wondering... i know a void* turns a variable into raw binary data. is it possible to have a void* be a pointer to another variable?


also, how would i store the address of the variables as a pointer?

int *p=0;//obviously

then store the address of p, so

a= point to p;

a=1;

//p is now 1

Share this post


Link to post
Share on other sites
Advertisement
void pointer simply means that an undefined type.
You can typecast it in whatever you like. (also all other types)

Example:
int* pixelArray = (int*)voidPtr;
Usually its a bad practice.. you must always think what your data is and what you do with it. But in some cases it can be unavoidable.

Pointer is an address.
So if you have a pointer of A, it means that the pointer contains the memory address where A is stored.

Share this post


Link to post
Share on other sites
i see. why is it bad practice? im on the verge of using it where i need different types for the same variable, which is alot.
so this works?

bool a=true;

void foo(void* val)
{
//found its a bool
bool* nVal=(bool*)val;
if(nval==true){nval=false;}
}

foo((void*)&a);

Share this post


Link to post
Share on other sites
It works, yeah, but the real problem isn't the fact that you're using a void*. The real problem is that you need to do it a lot. That signals a design flaw in your program.

Share this post


Link to post
Share on other sites
one of the examples if you try to delete void pointer that points to a class it wont call the destructor of that class.
It also means that you have questionable architecture.

Can you tell me why do you want to use it and for what?

Share this post


Link to post
Share on other sites
XD im writing some code for a menu button, which has to either point to a bool, int, float or string/char array somewhere in the program. it would be alot easier to have it point to a variable and tell it the class than to make seperate buttons for each variable.

Share this post


Link to post
Share on other sites
Alternatively you can use a discriminate union, such as boost::variant. You can see a sample of using boost::variant here

Share this post


Link to post
Share on other sites

Its cleaner to use inheritance in this case.
Button that is derived specific per type.


I'd probably use templates or a union if i need a class dealing with multiple parameter types. in C++ a discriminated union such as boost::variant is prefered over a normal C style union due to the added type safety, C unions however work well enough if you do something similar to the SDL_Event union, (a union of a 1 byte type identifier and a set of structs where the structs all start with a 1 byte type identifier aswell).

For a GUI a signals/slots, event or callback mechanism would be preferable to having buttons with pointers to data owned by other parts of the program.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!