Page 1 of 1

So... I found out about void pointers today...

Posted: Mon Oct 11, 2010 8:13 pm
by epicasian
My question is, how can you use (if you even can) void pointers in a game engine to abstract the custom data type being passed into a function?

I saw a function like this:

Code: Select all

void printIntX(void* argX)
{
	int a;
	a = *(int *) argX;
	cout << a;
}
Is there a way that you can do this, without creating functions like:

Code: Select all

void useInputComponent(void* argInput)
{	
        InputComponent a;
	a = *(InputComponent *) argInput;
        //blah
}

void useRenderComponent(void* argRender)
{	
        RenderComponent a;
	a = *(RenderComponent *) argRender;
        //blah blah
}
Thanks in advance, and sorry if I didn't explain myself very well.

Re: So... I found out about void pointers today...

Posted: Mon Oct 11, 2010 8:26 pm
by avansc
look at boost::any

but yeah, Abstraction in C is possible, but its usually a case where you have to create the casts your self. Usually a well designed project will do fine with this approach.

Re: So... I found out about void pointers today...

Posted: Mon Oct 11, 2010 8:55 pm
by epicasian
Sweet, thanks for the fast response. I have another question though.

Does this code:

Code: Select all

a = *(int *) argX;
make a = a dereferenced integer cast of argX? I'm asking this because I do't want to copy-paste the code sample and have no idea what it means.

Thanks again.

Re: So... I found out about void pointers today...

Posted: Mon Oct 11, 2010 9:46 pm
by avansc
the (int*)arg takes the void pointer and casts it to a int pointer, the * on the front dereferences that pointer.

Re: So... I found out about void pointers today...

Posted: Tue Oct 12, 2010 9:19 am
by dandymcgee
epicasian wrote: Is there a way that you can do this, without creating functions like:

Code: Select all

void useInputComponent(void* argInput)
{	
        InputComponent a;
	a = *(InputComponent *) argInput;
        //blah
}

void useRenderComponent(void* argRender)
{	
        RenderComponent a;
	a = *(RenderComponent *) argRender;
        //blah blah
}
If using C++, this is precisely what templates were designed for.