Home
C++ Keywords: typedef
An Alias for a Known Type
In C++, you can declare a variable using one of the built-indata 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. Youcan 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 manydeclarations, you can customize its name. In C++, thetypedef keywordallows you to create an alias for a data type. The formula to follow is:
typedef [attributes] DataType AliasName;
The typedef keyword is required. The attributeis not.
The typedef keyword can be followed by any C++ built-in datatype, includingint, short, signed, unsigned,char,signed char, unsigned char,double, long, or longdouble. The data type can also be an existing class that either is providedby one of the libraries that ship with the C++ compiler. For example, it can bethestring 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 willbe 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;
}
In typedef short SmallNumber, SmallNumber can now be used as a data type to declare a variable for a small integer.
In the second example, typedef unsigned int Positive, The Positive word can then be used to declare a variable of anunsigned int type.
After typedef double* PDouble, the word PDouble can be used to declare a pointer to double.
By defining typedef string FiveStrings[5], FiveStrings can be used to declare an array of 5 strings with each string being of typestring.
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 aspublic, 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 FunctionX, Inc.
c/c++ typedef
最新推荐文章于 2024-09-14 21:14:29 发布