【c++】Usage Of Void(English)

#include<bits/stdc++.h>
using namespace std;
//this program is about the usage of void in language C++
//[usage of void]
//1when func takes no arguments ,use void
//default_return_type func(void)
//2when func return no values, use void
//void func(parameters)
//[usage of void*]
//1.void* can point to any types of data, which means void* can be assigned
//by any types of data
int main(){
    char *p_char = new char[10];
    memset(p_char,'1',sizeof(p_char));
    void *p_void;
    p_void = p_char;
    char* p_char1 = (char*)p_void;
    cout << *p_char1;
//    cout << *(((char*)p_void)++); illegal
    return 0;
}

//2.when you wanna get the address storaged in the p_void, you should use casting methods like (int*)p_void
//3.void* is not a pointer-to-object type, which means that *p_void is illegal
//the reason is that void* only konw the starting address of the data in the me
//mory but void* don't know the size of the data, unless int(takes four bytes) or other self-defined
//class types  
//4.from 3 above we know that p_void += n is incorrect(no size of data is given)
//and ANSI think so, however GUN don't think so,they think that the void* should has the
//same size as char*(1 byte) for no reason

猜你喜欢

转载自blog.csdn.net/chenhanxuan1999/article/details/82973695