Simplifying with typedef
C++ does provide tools other than auto for simplifying declarations. You may recall from Chapter 5, “Loops and Relational Expressions,” that the typedef keyword allows you to create a type alias:
typedef double real; // makes real another name for double
The technique is to declare the alias as if it were an identifier and to insert the keyword typedef at the beginning. So you can do this to make p_fun an alias for the function pointer type used in Listing 7.19:
typedef const double *(*p_fun)(const double *, int); // p_fun now a type namep_fun p1 = f1; // p1 points to the f1() function
You then can use this type to build elaborations:
p_fun pa[3] = {f1,f2,f3}; // pa an array of 3 function pointersp_fun (*pd)[3] = &pa; // ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access