C ++ type declaration for C ++

Could you explain what the next line means?

typedef int (*Callback)(void * const param,int s)
+3
source share
3 answers

This means that Callback- this is a new name for the type: a pointer to a function that returns an int and accepts two parameters of the type "const pointer to void" and "int".

For function f:

int f(void * const param, int s)
{
    /* ... */
}

Callbackcan be used to store a pointer to f:

Callback c = &f;

A function fcan later be called via a pointer without directly accessing its name:

int result = c(NULL, 0);

The name is fnot displayed at the dial peer .

+6
source

"" , , int, : a void* const int. , , .., :

int fn(void * const param,int s) { ... }

Callback p;
p = fn;
int x = p(NULL, 38);

: typedef ... typedef , ..

+2

It declares a function type:

// Set up Callback as a type that represents a function pointer
typedef int (*Callback)(void * const param,int s);

// A function that matches the Callback type
int myFunction(void* const param,int s)
{
    // STUFF
    return 1;
}

int main()
{
    // declare a variable and assign to it.
    Callback   funcPtr = &myFunction;
}
+2
source

Source: https://habr.com/ru/post/1778906/


All Articles