Search code examples
c++gccfunction-pointers

How do I default initialize function pointer in C++?


I am passing a function pointer to another function, and I want it to be default initialized, and this is what I am trying, but this gives syntax error

void bar(int i) {}
void foo(void (*default_bar=bar)(int)) {
//
}

The error :

Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/f_13.d" -MT"src/BinarySearchTree_13.d" -o "src/BinarySearchTree_13.o" "../src/f_13.cpp"
In file included from ../src/f_13.cpp:10:
../src/tree.h:51: error: expected `)' before '=' token
../src/tree.h:51: error: 'foo' declared as function returning a function
../src/tree.h:51: error: expected ';' before ')' token
../src/tree.h:60: error: expected `;' before 'void'
make: *** [src/f_13.o] Error 1

Just a point that this works fine :

void foo(void (*default_bar)(int)) {

Solution

  • This way:

    void foo(void (*default_bar)(int) = bar)