What does it mean when Function pointer type is used as a function?

Viewed 45

In the following C++ code:

typedef void (*FuncPtr)(void);    // FuncPtr typedef

void foo(void){...}               // some function

void bar(FuncPtr ptr){...}        // take FuncPtr type as an argument

void main(void)
{
    bar(FuncPtr(foo));            // where bar is used
}

What does FuncPtr(foo) mean in calling bar?

Is this a way just to cast foo to FuncPtr type? But why not use (FuncPtr)foo?

And is this a feature only in C++, or both in C?

1 Answers

FuncPtr(foo) casts foo to a FuncPtr. It does the same thing as (FuncPtr)foo.

foo already has the correct type. So, bar(foo); does the job and should be preferred.

FuncPtr(foo) is only valid in C++. (FuncPtr)foo is valid in both languages.

Related