Code
void printA()
{
cout << "A\n";
}
void print1( void (*func_ptr)(void) )
{
func_ptr();
}
void print2( void func(void))
{
func();
}
int main()
{
//Method 1a (passing function pointer)
print1(&printA);
//Method 1b (why does it work even though I didn't pass a reference or a pointer?)
print1(printA);
//Method 2 (passing the whole function ?)
print2(printA);
return 0;
}
Method 1a makes sense: print1 was expecting a reference or pointer.
Question 1:
Method 1b does not make sense: Why doesn't the compiler throw an error. Why does it work, even though I didn't pass the reference or the pointer of printA() to print1()?
Question 2:
Method 2a implies a new copy of the function printA() is made and passed to function print2().
In other words, is the function printA() is passed to function print2() by value?
Question 3:
Are identifiers of functions references? (this might help)
Much appreciated thanks.