Home > Articles

This chapter is from the book

Using typedef to Substitute a Variable’s Type

C++ enables you to substitute variable types with something that you might find convenient. You use the keyword typedef for that. Here is an example where a programmer wants to call an unsigned int a descriptive STRICTLY_POSITIVE_INTEGER:

typedef unsigned int STRICTLY_POSITIVE_INTEGER;
STRICTLY_POSITIVE_INTEGER numEggsInBasket = 45;

When this code is compiled, the first line tells the compiler that a STRICTLY_POSITIVE_INTEGER is nothing but an unsigned int. At later stages, when the compiler encounters the already defined type STRICTLY_POSITIVE_INTEGER, it substitutes it for unsigned int and continues compilation.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.