void as a parameter type
In the book, "Windows Game Programming For Dummmies" by LaMothe, the author makes reference to this function definition
Game_Init(void *params = NULL) {...}
Now I know that *params is a pointer that is given the default value of NULL, but what is the type void? In C++, function() is the same as function(void), but I''ve never seen void as a variable type. Any thoughts?
JaRoS
Ahem, void* is the type, and params the variable.
void* is a pointer to an unspecified type, to a blob of memory, the kind returned by malloc().
void* is a pointer to an unspecified type, to a blob of memory, the kind returned by malloc().
(void * params) means that params is a pointer to a type that has not been specified yet. Normally, the pointer can be cast to a specific type as required e.g.
DoSomething((int *)params);
DoSomething((int *)params);
All it is is a void pointer, a pointer to any type of object can be assigned to a void pointer. But to be used it must be cast into a specific type. Now it is unsafe to use a pointer that has been cast to a type that differs from the type the object pointed to originally.
Ex:
void( int* foo) {
void* bar = foo; //implicit conversion from int* to void*
int* var = static_cast(bar); // explicit conversion back to int ..fine
double* var2 = static_cast(bar); // unsafe
}
Void pointers are used as function parameters when you cannot make assumptions about the type of object and for returning untyped objects.
Since LaMothe could not assume you would be sending anything in to Game_Init as a parameter and if you did what type it would be he made it a void pointer and made it default to NULL.
Hope that helps ya out a bit.
Ex:
void( int* foo) {
void* bar = foo; //implicit conversion from int* to void*
int* var = static_cast(bar); // explicit conversion back to int ..fine
double* var2 = static_cast(bar); // unsafe
}
Void pointers are used as function parameters when you cannot make assumptions about the type of object and for returning untyped objects.
Since LaMothe could not assume you would be sending anything in to Game_Init as a parameter and if you did what type it would be he made it a void pointer and made it default to NULL.
Hope that helps ya out a bit.
Thanx for the prompt reponce! Much appreciated...
So if I understand what your saying, you can pretty much assign any type pointer to a void pointer? So, back to my origional example, if i wanted to pass a int pointer into Game_Init() it would look something like this:
{
...
int* p;
...
Game_Init((int*) p);
...
}
Would this be correct? or Do I not need to specifically cast the pointer p in the function call?
JaRoS
So if I understand what your saying, you can pretty much assign any type pointer to a void pointer? So, back to my origional example, if i wanted to pass a int pointer into Game_Init() it would look something like this:
{
...
int* p;
...
Game_Init((int*) p);
...
}
Would this be correct? or Do I not need to specifically cast the pointer p in the function call?
JaRoS
When you see pointers declared like this...
...and references declared like this...
...I think it''s very confusing as to what the type actually is. For instance, the first example looks like the type is int, when it''s actually an integer pointer. So, it should be written like this:
This reads in English as "integer pointer ''a''", which is what a really is.
As for references, the second example, having the ampersand before the variable name looks like you''re taking the address of the variable instead of declaring a reference. Of course, by context you can figure out that you''re definately not taking the variable''s address, but it''s still confusing upon first glance. So, do it like this:
This reads in English as "integer reference ''a''".
Needless to say, this is all a matter of personal preference...
In JaRoS''s example, he said that *params was the pointer, but that''s definately not the case, even though the declaration style makes it look like that. params is the pointer and *params is the object of the pointer params. Yet another reason to use the int* and int& style...
I''m done ranting.
int *a;
...and references declared like this...
int &a
...I think it''s very confusing as to what the type actually is. For instance, the first example looks like the type is int, when it''s actually an integer pointer. So, it should be written like this:
int* a;
This reads in English as "integer pointer ''a''", which is what a really is.
As for references, the second example, having the ampersand before the variable name looks like you''re taking the address of the variable instead of declaring a reference. Of course, by context you can figure out that you''re definately not taking the variable''s address, but it''s still confusing upon first glance. So, do it like this:
int& a;
This reads in English as "integer reference ''a''".
Needless to say, this is all a matter of personal preference...
In JaRoS''s example, he said that *params was the pointer, but that''s definately not the case, even though the declaration style makes it look like that. params is the pointer and *params is the object of the pointer params. Yet another reason to use the int* and int& style...
I''m done ranting.
quote:Original post by merlin9x9
Needless to say, this is all a matter of personal preference...
Exactly. The arguement against that is this:
int* a, b;
What is a? It's an integer pointer. What is b? It's an integer. Typing it like that makes it appear that both are integer pointers even though they both aren't. That's the main reason I prefer to type it like this:
int *a, *b;
Now they both are actually integer pointers.
[Resist Windows XP's Invasive Production Activation Technology!]
Edited by - Null and Void on December 2, 2001 10:12:05 PM
quote:Original post by JaRoS
Thanx for the prompt reponce! Much appreciated...
So if I understand what your saying, you can pretty much assign any type pointer to a void pointer? So, back to my origional example, if i wanted to pass a int pointer into Game_Init() it would look something like this:
{
...
int* p;
...
Game_Init((int*) p);
...
}
Would this be correct? or Do I not need to specifically cast the pointer p in the function call?
JaRoS
Close:
{
...
int* p;
...
Game_Init((void*) p);
...
}
being able to declare an int pointer and an int in the same declartion statement is a defect in the language. Afterall I can''t say int static a, b; and get static a, member b. Or can I? I sure hope not.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement