Typedef and TypeName keywords

zhaozj2021-02-17  96

1, Type Description Typedef

The format of the type of type is: typedef type definition name; Type Description Only a new name of a data type is defined instead of defining a new data type. Define names indicating this type of new name. For example: use the following statement to define a new name: typedef int linened_int; After use instructions, Signed_INT has become the synonym of int, which can be used to define integer variables with SIGNED_INT. For example: Signed_INT I, J; (equivalent to INT I, J). But Long Signed_INT I, J; is illegal. TypedEf can also be used to explain the structure, joint, and enumerations and classes. Description A structure is: typedef struct {data type member name; data type member name; ...} Structure name; at this time, the structural variable can be defined directly with the structural name. For example: TypedEf struct {char name [8]; int class; char Subclass [6]; float math, phys, cym, eNGL, BIOL;} student; student liuqi; then LiUQi is defined as structural arrays and structural pointers. 2, type explanation TYPENAME

The TypenAme keyword tells the compiler to explain a special name into a type, and must use the TypenAme keyword to a NAME in the following cases: 1. A unique name (can be understood as a type of understanding), it is nested in another type. 2. Depending on a template parameter, it is said that template parameters contain this Name to some extent. When the template parameter makes the compiler have misunderstand when a type is recognized.

During insurance, you should use TypeName as a variable in all compilers. Just like the T :: ID in the example above, because we use Typename, so the compiler knows that it is a type, which can be used to declare and create an instance.

Give you a concise usage guide: If your type is limited in the template parameter, you must use Typename.

#include #include // for typeid () operatorusing namespace std; template struct COne {// default member is public typedef TP one_value_type;}; template // with a template Class as a template parameter, this is a very common struct ctwo {// please pay attention to the following two rows // typef conong: one_value_type two_value_type; // * 1 TypedEf TypeName Cone: One_Value_type two_value_type; // * 2}; // More than two Template category only defines two internal public types, but please pay attention to the second class CTWO's Two_Value_type type // Rely on the CONE's one_value_type, and the latter depends on the parameter type when the CONE template class is instantiated .int main. ) {Typef cone oneint_type; typed_type> twoint_type; twoint_type :: two_value_type i; int J; if (typeid (i) == typeid (j)) // If i is an int type variable cout << Right! "<< Endl; // Print Right Return;} The above examples are compiled with G 2.93 in Linux, and" Right "is printed. But if the * 1 row is removed, the comment * 2 line, then compile Reporting an error, the compiler does not know what is: one_value_type? What is usually revealed after the type in the template class parameter is instantiated, but this CTWO class is biased to rely on an existing CONE template class, I hope to pre-guarantee CTWO :: Two_Value_type and cone: One_Value belongs to the same type, this is, please ask TypeName to go out, tell the compiler, back CONE: One_Value_type is an existing place Type name, such a compiler can work well. Use Typename to replace Class

After detailing the use of Typename, we can now select TypenAme to replace the Class declaration, which can increase the clarity of the program. //: c03: usingtypename.cpp

// using 'Typename' in The Template Argument List

Template Class X {};

int main ()

{

X x;

} ///: ~

You will of course see that many similar code do not use the TypenAme keyword, because the template concept has been born for a long time, there is a TypenAme keyword.

Customize a type with Typename

To know the TypeName keyword not automatically type, TypeName SEQ :: Iterator it;

Just declares a variable of a SEQ :: Iterator type, if you want to define a new type, you have this: typedef typeename seq :: itemarator it;

转载请注明原文地址:https://www.9cbs.com/read-31283.html

New Post(0)