do this
void* vp;
int* ip;
ip=(int*)vp;
In c++,why can't we assign a void pointer to an int type pointer?
Because they're different data types, one is a pointer to an int, and the other is not :-) They're all just memory addresses though, so if you know what you're doing then you can typecast one to the other.
Reply:Because doing so, without a really good reason, is a very bad idea. The compiler warning / error for that attempted assignment is a safeguard to keep programmers from making silly mistakes. If you get this kind of a message, it is very likely that your program's data design is flawed or poorly thought out.
However, C++ will allow you to make that kind of assignment using an explicit typecast. Using a typecast is the same as telling the compiler that you know what you are doing, and take full responsibility for the consequences of that kind of assignment and any side effects which may occur.
elephant ear
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment