O'Reilly logo

Imperfect C++ Practical Solutions for Real-Life Programming by Matthew Wilson

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

Chapter 18. Typedefs

The typedef specifier in C and C++ provides a new name for a given type. Two classic uses in C are in removing confusion when using function pointers and reducing typing effort when using structures. Consider the code without typedefs in Listing 18.1.

Example 18.1. 

// Info.h
struct Info
{};
int process(int (*)(struct Info*, int*), int*);

// client_code.cpp
int process_forwards(struct Info*, int);
int process_backwards(struct Info*, int);

int main()
{
  struct Info info;
  int (*pfn[10])(struct Info*, int);
  . . .
  for(i = 0; . . .; ++i)
  {
    pfn[i] = . . .

Contrast that with the code in Listing 18.2 that uses structure and function pointer typedefs.

Example 18.2. 

// Info.h
typedef struct Info {} Info_t;              // struct typedef
typedef int ...

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