Today in writing the project needs, use to void*, began not to understand what void* mean, asked the eldest, a language awakened dream of people!!!!!!
Event->setuserdata ((void*) 10);
int* data = (int*) event->getuserdata ();
Cclog ("data =%d", data);
Start so set Setuserdata, very puzzled why not *data can output 10, but *data on the error!!!!
The boss said, the value of the pointer itself is 10, I understand!!!!
(void*) 10 is to turn 10 into a pointer address value, this address is not initialized, so *data is basically wrong, the address value is 10 of this memory address content unknown!!!!
Later the boss said, direct int data = (int) event->getuserdata (); You can use the value of 10, as long as the compiler does not error!!!
In the group also asked this question, also get a solution, directly new out on OK, there will be no ambiguity!
Event->setuserdata (new int (10));
int* data = (int*) event->getuserdata ();
int b = *data;
Cclog ("&data =%d, data =%d", data, *data);
This can also use the value of 10!!!
Void* This previous use of less, do not understand!!!! Today is a rise in knowledge!!!!!!!
If it is an int type, it is convenient to use the first kind!!!!!
Thank you very much boss and group of Friends!!!!
This article is from the "Little Wolf zer" blog, please make sure to keep this source http://9151482.blog.51cto.com/9141482/1637196
Experience with C + + void*