yoyozi
yoyozi

Reputation: 96

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

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?

Upvotes: 0

Views: 220

Answers (1)

mch
mch

Reputation: 9804

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.

Upvotes: 2

Related Questions