Ciò significa che se hai una funzione, dicono f()
come:
void f(){}
allora ecco cosa si può fare e cosa non si può fare: il codice
FunctionType fun1 = f; //error - cannot create object of function type
FunctionType *fun2 = f; //ok
FunctionTypeR fun3 = f; //ok
Test:
typedef void (&FunctionTypeR)();
typedef void FunctionType();
void f(){}
int main() {
FunctionType fun1 = f; //error - cannot create object of function type
FunctionType *fun2 = f; //ok
FunctionTypeR fun3 = f; //ok
return 0;
}
Vedere ora l'errore di compilazione (e le avvertenze):
prog.cpp: In function ‘int main()’:
prog.cpp:7: error: function ‘void fun1()’ is initialized like a variable
prog.cpp:8: warning: unused variable ‘fun2’
prog.cpp:9: warning: unused variable ‘fun3’
Demo in linea: http://ideone.com/hpTEv
Tuttavia, se si utilizza FunctionType
(che è un tipo di funzione) in una lista di parametri funzione di:
void foo(FunctionType bar);
allora è equivalente a
void foo(FunctionType * bar);
Ciò significa, n O Matter ciò che si scrive, è possibile chiamare la funzione utilizzando bar
come:
bar(); //ok
(*bar)(); //ok
Cioè, è possibile scrivere questo:
void h(FunctionType fun) { fun(); }
void g(FunctionType fun) { (*fun)(); }
Demo: http://ideone.com/kwUE9
Ciò è dovuto al funzione digitare a funzione tipo puntatore regolazione; vale a dire, il tipo di funzione di è regolato per diventare un puntatore di funzionare Tipo:
Function type | Function pointer type (adjusted type)
void() | void (*)()
void (int) | void (*)(int)
int (int,int) | int (*)(int,int)
.... | ... so on
Il C++ 03 standard dice in §13.1/3, dichiarazioni
parametri che differiscono solo che uno è un tipo di funzione e l'altro è un puntatore alla funzione stesso tipo sono equivalente. In altre parole, il tipo di funzione viene regolato per diventare un puntatore al tipo di funzione (8.3.5).
[Example:
void h(int());
void h(int (*)()); // redeclaration of h(int())
void h(int x()) { } // definition of h(int())
void h(int (*x)()) { } // ill-formed: redefinition of h(int())
]
E se si utilizza `FunctionTypeR
(che è una funzione di riferimento tipo) come:
void foo(FunctionTypeR bar);
allora è equivalente a:
void foo(FunctionType * & bar);
E,
void h(FunctionTypeR fun) { fun(); }
void g(FunctionTypeR fun) { (*fun)(); }
Demo: parte http://ideone.com/SmtQv
Interessante ...
È possibile utilizzare FunctionType per dichiarare una funzione (ma non definirlo).
Per esempio,
struct A
{
//member function declaration.
FunctionType f; //equivalent to : void f();
};
void A::f() //definition
{
std::cout << "haha" << std::endl;
}
//forward declaration
FunctionType h; //equivalent to : void h();
int main() {
A a;
a.f(); //call member function
h(); //call non-member function
}
void h() //definition goes below main()
{
std::cout <<"hmmm.." << std::endl;
}
Demo: http://ideone.com/W4ED2
significa void foo (bar FunctionType) è in realtà foo (FunctionType & bar)? che è equivalente a foo (barra FunctionTypeR)? –
No, è un errore. – Shahbaz
void 'foo (barra FunctionType)' non è un errore. compila e corre. L'errore è "FunctionType fun1 = f"; –