首页 > 代码库 > 一切为了笔试~

一切为了笔试~

 1 //sizeof的使用
 2     char a[30]="abcde";
 3     char *b=new char[20];
 4     cout<<sizeof(a)<<endl;//30
 5     cout<<sizeof(b)<<endl;//4
 6     cout<<sizeof(a[3])<<a[3]<<endl;//1
 7     cout<<sizeof(b+3)<<endl;//4
 8     cout<<sizeof(*(b+4))<<endl;//1
 9 
10     class A  
11     {    
12     };  
13 
14     class B  
15     {  
16     };  
17 
18     class C  
19     {  
20     };  
21     class G
22     {};
23 
24     class D:public A
25     {  
26     };  
27 
28     class E:public A,B  
29     {  
30     };  
31 
32     class F:public A,B,C,G  
33     {  
34     };  
35     cout<<"void (D::*)()的大小是:"<<sizeofvoid (D::*)() )<<endl; // 4    
36     cout<<"void (E::*)()的大小是:"<<sizeofvoid (E::*)() )<<endl; // 8    
37     cout<<"void (F::*)()的大小是:"<<sizeofvoid (F::*)() )<<endl; // 8   
38     cout<<"void* 的大小是:"<<sizeofvoid(*) )<<endl;  //4 

 

 

int test(int j)
{
    static int i=10;
    i=i+j;
    return i;
}
//static的使用
    cout<<test(12)<<endl;//22

  cout<<test(4)<<endl;//26 

 

  1 class A
2

 {
3     int x;
4     double d;
5 };
6 A a[10];
7 cout << sizeof(A) << endl;      // 输出:16,此处有内存补齐,所以不是12
8 cout << sizeof(a) << endl;      // 输出:160

一切为了笔试~