Home

C++ Keywords: typedef

 

An Alias for a Known Type

In C++, you can declare a variable using one of the built-in data types:

#include <iostream>

using namespace std;



int main()

{

	int number = 248;

	return 0;

}

In the same way, you can declare an array or a pointer. You can also declare various variables on the same line. Here are examples:

#include <iostream>

#include <string>

using namespace std;



int main()

{

	int number = 248;

	double *distance = new double(390.82);

	string countries[] = { "Ghana", "Angola", "Togo",

	                       "Tunisia", "Cote d'Ivoire" };



	cout << "Number: " << number << 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;

}

If you plan to use the same type of data for many declarations, you can customize its name. In C++, the typedef keyword allows you to create an alias for a data type. The formula to follow is:

typedef [attributes] DataType AliasName;

The typedef keyword is required. The attribute is not.

The typedef keyword can be followed by any C++ built-in data type, including int, short, signed, unsigned, char, signed char, unsigned char, double, long, or long double. The data type can also be an existing class that either is provided by one of the libraries that ship with the C++ compiler. For example, it can be the string class. The data type can also be a pointer to a known type.

On the right side of the data type, type the name that will be used to represent the data type or the pointer. Here are examples:

#include <iostream>

#include <string>

using namespace std;



typedef short SmallNumber;

typedef unsigned int Positive;

typedef double* PDouble;

typedef string FiveStrings[5];



int main()

{



	cout << endl;

	return 0;

}
  1. In typedef short SmallNumber, SmallNumber can now be used as a data type to declare a variable for a small integer.
  2. In the second example, typedef unsigned int Positive, The Positive word can then be used to declare a variable of an unsigned int type.
  3. After typedef double* PDouble, the word PDouble can be used to declare a pointer to double.
  4. By defining typedef string FiveStrings[5], FiveStrings can be used to declare an array of 5 strings with each string being of type string.

Based on this rule, you can declare the variables based on types you have typed defined. Here are examples:

#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;

}

This would produce:

Temperature: -248

Height:      1048

Distance:    390.82

Countries:

        Ghana

        Angola

        Togo

        Tunisia

        Cote d'Ivoire

Type-Defining a Function

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. Another rule is that the definition must specify that it is a function pointer. Here is an example:

#include <iostream>

#include <string>

using namespace std;



typedef short SmallNumber;

typedef unsigned int Positive;

typedef double* PDouble;

typedef string FiveStrings[5];

typedef double (*Addition)(double value1, double value2);



double Add(double x, double y)

{

	double result = x + y;

	return result;

}



int main()

{

	SmallNumber temperature = -248;

	Positive height = 1048;

	PDouble distance = new double(390.82);

	FiveStrings countries = { "Ghana", "Angola", "Togo",

		                      "Tunisia", "Cote d'Ivoire" };

	Addition plus;



	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;



	plus = Add;

	cout << "3855.06 + 74.88 = " << plus(3855.06, 74.88) << endl;

	return 0;

}

This would produce:

Temperature: -248

Height:      1048

Distance:    390.82

Countries:

        Ghana

        Angola

        Togo

        Tunisia

        Cote d'Ivoire

3855.06 + 74.88 = 3929.94

The Attribute of a Type-Definition

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. Here are examples:

#include <iostream>

#include <string>

using namespace std;



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);



double Add(double x, double y)

{

	double result = x + y;

	return result;

}



int main()

{

	SmallNumber temperature = -248;

	Positive height = 1048;

	PDouble distance = new double(390.82);

	FiveStrings countries = { "Ghana", "Angola", "Togo",

		                  "Tunisia", "Cote d'Ivoire" };

	Addition plus;



	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;



	plus = Add;

	cout << "3855.06 + 74.88 = " << plus(3855.06, 74.88) << endl << endl;

	return 0;

}

In C++, the attribute of the typedef is usually applied as a class, a struct, or an enum. Here are examples:

#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;

}Employee;



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;

}

 

 

Home Copyright © 2005-2016, FunctionX