Visualizzazione dei risultati da 1 a 3 su 3

Discussione: [c++]Template

  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c++]Template

    mi sono rimesso a spulciare alcuni libri che avevo letto per consolidare .
    ho questo codice tratto da thinking c++:

    codice:
    using namespace std;
    
    template <typename T> void f(T*) {}
    void h(void (*pf)(int*)) {}
    
    template <class T>
    void g(void (*pf)(T*)) {}
    int _tmain(int argc, _TCHAR* argv[])
    {
    	// Full type exposition:
    	h(&f<int>);
    	// Type induction:
    	h(&f);
    	// Full type exposition:
    	g<int>(&f<int>);
    	// Type inductions:
    	g(&f<int>);
    	g<int>(&f);
    	
    } ///:~
    due domande:
    1)void h(void (*pf)(int*)) {} dichiara un puntatore alla funzione f ?ma perchè questa sintassi , non riesco bene a comprendere.
    2)se ho una classe template e devo dichiarare un puntatore a funzione membro non dovrei utilizzare la sintassi oggetto.*funzione?
    faccio un po fatica a comprendere il meccanismo di quest esempio , chiedo a voi.
    ciao.

    grazie.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    1) Beh, perchè a suo tempo Kernighan e Ritchie decisero così.
    2) Li non c'è nessuna classe template, solo funzioni; quindi la sintassi oggetto::*puntatore_funzione_membro non si può applicare.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3

    Re: [c++]Template

    Originariamente inviato da giuseppe500

    1)void h(void (*pf)(int*)) {} dichiara un puntatore alla funzione f ?ma perchè questa sintassi , non riesco bene a comprendere.
    scusa ma a me sembra che con:
    codice:
    void h(void (*pf)(int*)) {}
    non venga dichiarato alcun puntatore alla funzione f, ma bensì venga definita la funzione h di tipo void che ha per argomento un puntatore a funzione (di tipo void che ha per argomento un puntatore a int).

    Facciamo un prova giusto per conferma:
    codice:
    /*
    
    Isidoro Ghezzi
    
    $ g++ --version; g++ -ansi -pedantic -Wextra -Wconversion main.cpp; ./a.out 
    i686-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
    Copyright (C) 2005 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    The value is: 2010;
    
    */
    
    #include <iostream>
    
    void test (int * thePtr){
    	std::cout << "The value is: " << (*thePtr) << ";" << std::endl;
    }
    
    void h(void (*pf)(int*)){
    	int a = 2010;
    	pf (&a);
    }
    
    int main (void){
    	h (test);
    	return 0;
    }
    confermato ;-)

    EDIT:
    che è equivalente a (in modo più leggibile ricorrendo al typedef):
    codice:
    /*
    
    Isidoro Ghezzi
    
    $ g++ --version; g++ -ansi -pedantic -Wextra -Wconversion main.cpp; ./a.out 
    i686-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
    Copyright (C) 2005 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    The value is: 2010;
    
    */
    
    #include <iostream>
    
    void test (int * thePtr){
    	std::cout << "The value is: " << (*thePtr) << ";" << std::endl;
    }
    
    typedef void (*PointerToAFunctionWithAnIntegerPointerAsArg) (int * theIntegerPointer);
    
    void h(PointerToAFunctionWithAnIntegerPointerAsArg pf){
    	int a = 2010;
    	pf (&a);
    }
    
    int main (void){
    	h (test);
    	return 0;
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.