O'Reilly logo

C++ Primer Plus by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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;     // ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required