Home > Software design >  What does "typedef void (*wskfun)(const string)" mean?
What does "typedef void (*wskfun)(const string)" mean?

Time:01-29

What does typedef void (*wskfun)(const string) mean, and why it is necessery to use it in creating structure if we want to attach function to the single node of node list?

typedef void (*wskfun)(const string);

struct w2k {
    w2k* next; 
    w2k* prev;

    string nazwa;

    wskfun fun;
};

CodePudding user response:

This typedef

typedef void (*wskfun)(const string);

declares a pointer to a function that has the return void and one argument of the type const string.

Without the typedef the structure will look the following way

struct w2k {
    w2k* next; 
    w2k* prev;

    string nazwa;

    void ( *fun )( const string );
};

Instead of the typedef of a function pointer the code will be more readable if you will declare a typedef for the function type like for example

typedef void wskfun(const string);

struct w2k {
    w2k* next; 
    w2k* prev;

    string nazwa;

    wskfun *fun;
};

If it is a C code then it is better to accept an object of the type std::string by reference. That is the typedef will look like

typedef void wskfun(const string &);

If it is a C code then it seems the name string in turn denotes a typedef name like

typedef char *string;

In this case using the specification const string does not make a great sense. It will be better to write

typedef void wskfun(const char *);

because const string means char * const instead of const char *.

  • Related