Skip to Content
Imperfect C++ Practical Solutions for Real-Life Programming
book

Imperfect C++ Practical Solutions for Real-Life Programming

by Matthew Wilson
October 2004
Intermediate to advanced
624 pages
16h 2m
English
Addison-Wesley Professional
Content preview from Imperfect C++ Practical Solutions for Real-Life Programming

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 ...
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.
Start your free trial

You might also like

Practical C++ Metaprogramming

Practical C++ Metaprogramming

Edouard Alligand, Joel Falcou

Publisher Resources

ISBN: 0321228774Purchase book