C++ Void* Questions

Started by
18 comments, last by BennettSteele 12 years, 1 month ago
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
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.
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);
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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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?
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.
Its cleaner to use inheritance in this case.
Button that is derived specific per type.
:-\ i read the book i learned c++ from (1200 pages) and i guess i forgot about some stuff.. time to hit the books. :P
Alternatively you can use a discriminate union, such as boost::variant. You can see a sample of using boost::variant here

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


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.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement