C++ keyword used to declare an enumeration. The typedef keyword can be followed by any C++ built-in data type or any class. Also the data type can be a pointer to a known type.


Example 1
A file, might be open in one of three states: input, output, and append.

One way of do that:

const int input = 0;
const int output = 1;
const int append = 2;

However, there is no indication that these values are related in any way.

Better way:

//input is 0, output is 1, and append is 2
enum open_modes {input, output, append};}}

By default, the first enumerator is assigned the value zero. Each subsequent enumerator is assigned a value one greater than the value of the enumerator that immediately precedes it.

Example 2

// shape is 1, sphere is 2, cylinder is 3, polygon is 4
enum Forms {shape = 1, sphere, cylinder, polygon};

It is not possible to change the value of an enumerator. As a consequence an enumerator is itself a constant expression and so can be used where a constant expression is required.

Each enum defines a unique type.


// point2d is 2, point2w is 3, point3d is 3, point3w is 4
enum Points { point2d = 2, point2w, point3d = 3, point3w };
Points pt3d = point3d;         //  ok: point3d is a Points enumerator
Points pt2w = 3;                  //  error: pt2w initialized with int
pt2w = polygon;                  //  error: polygon is not a Points enumerator
pt2w = pt3d;                       //  ok: both are objects of Points enum type

Example 4

#include <iostream>
#include <string>
using namespace std;
typedef short SmallNumber;
typedef unsigned int Positive;
typedef double* PDouble;
typedef string FiveStrings[5];
int main()
    SmallNumber temperature = -248;
    Positive height = 1048;
    PDouble distance = new double(390.82);
    FiveStrings countries = { "Ghana", "Angola", "Togo",
                          "Tunisia", "Cote d'Ivoire" };
    cout << "Temperature: " << temperature << endl;
    cout << "Height:      " << height << endl;
    cout << "Distance:    " << *distance << endl;
    cout << "Countries:\n";
    for(int i = 0; i < sizeof(countries) / sizeof(string); i++)
        cout << '\t' << countries[i] << endl;
    cout << endl;
    return 0;

The typedef keyword can also be used to define an alias for a function. In this case, you must specify the return type of the function and its type(s) of argument(s), if any. Also the definition must specify that it is a function pointer. More about function pointers see pointers-to-functions.

The typedef keyword can be followed by an attribute before the data type. In its simplest form, the attribute can be that of an access level such as public, private, or protected.

Example 5

#include <iostream>
#include <string>
using namespace std;
typedef struct Student;
typedef class Country;
typedef public short SmallNumber;
typedef private unsigned int Positive;
typedef protected double* PDouble;
typedef public string FiveStrings[5];
typedef private double (*Addition)(double value1, double value2);
struct Student
    string FirstName;
    string LastName;
typedef struct _Empl
    string FullName;
    double HourlySalary;
class Country
    string Name;
    string Capital;
    string Code;
double Add(double x, double y)
    double result = x + y;
    return result;
typedef enum EmplStatus { esFullTime, esPartTime, esContractor };
typedef Student *PStudent;
typedef Country *PCountry;
int main()
    Student pupil;
    Country pais;
    EmplStatus emplst;
    PStudent ptrStd = new Student;
    PCountry pPais = new Country;
    return 0;
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.