Experience with C + + void*

Source: Internet
Author: User

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*

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.