close

Sample 1 :

typedef int   (*cmp_f) ( const void *a, const void *b );

struct Mars
{
    int i ;
    int j ;
    cmp_f cmp ;
} ;

int Marscmp(const void *a, const void *b )
{
    int *p1 = (void*) a ;
    int *p2 = (void*) b ;
    if(*p1 > *p2)
        return 1 ;
    else
        return 0 ;
}

int main()
{
    struct Mars M1 ;
    M1.i = 100 ;
    M1.j = 90 ;
    M1.cmp = &Marscmp ;
    int iret ;
    iret = M1.cmp(&(M1.i),&(M1.j)) ;
    printf("(%d)\n",iret) ;
    M1.j = 101 ;
    iret = M1.cmp(&(M1.i),&(M1.j)) ;
    printf("(%d)\n",iret) ;

}

 

Sample 2 :

struct my_point_deleter {  
    void operator()(FILE* f) const 
 {
     cout << "my_point_deleter called !!" << endl ;
     fclose(f) ;
 }
};

void func(FILE *f)
{
    cout << "func called " << endl ;
    fclose(f) ;
}

int main()
{  
    #ifdef TEST1
    unique_ptr<FILE, void(*) (FILE*)> f(fopen("hanoi.c", "r"),[](FILE* f){cout << "going closed" << endl; fclose(f);});
 #endif
    #ifdef TEST2
    unique_ptr<FILE, my_point_deleter> f(fopen("hanoi.c", "r")) ;
 #endif
    #ifdef TEST3
    unique_ptr<FILE, void(*) (FILE*)> f(fopen("hanoi.c", "r"),&func) ;
 #endif

 
 char aux[100];  
 while (!feof(f.get())) //f.get returns me the actual plain old pointer  
 {     
     fgets(aux, 100, f.get());     
  cout << aux;  
 }     //No need to invoke fclose() because the unique_ptr will take care of it.
}

 

 

 

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()