12.3 typedef

I l @ ve RuBoard

C++ allows you to define your own variable types through the typedef statement. This provides a way for you to extend C++'s basic types. The general form of the typedef statement is:

 typedef   type-declaration;   

The type-declaration is the same as a variable declaration except a type name is used instead of a variable name . For example:

 typedef int width; // Define a type that is the width of an object 

defines a new type, width , that is the same as an integer. So the declaration:

 width box_width; 

is the same as:

 int box_width; 

At first glance, this is not much different from:

 #define width int  width box_width; 

However, typedef s can be used to define more complex objects that are beyond the scope of a simple #define statement, such as:

 typedef int group[10]; 

This statement defines a new type, group , which denotes an array of 10 integers. For example:

 int main(  )  {      typedef int group[10];     // Create a new type "group"     group totals;              // Use the new type for a variable     // Initialize each element of total     for (i = 0; i < 10; ++i)          totals[i] = 0; 
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net