Use defined type alias in the class
1 typedef defines type alias
1.1 How to use
In C++, the typedef keyword is used to define alias of a type.
typedef int MYINT;
At this time, MYINT is an alias for int, and other variables can be defined as a data type.
MYINT myint;
Among them, the type of myint is MYINT.
1.2 Notes
The typedef cannot be used to repeatedly define the type alias MYINT.
typedef int MYINT; typedef double MYINT;
At this time, the compiler reports an error, and the error message is "error C2371: "MYINT": Redefine; different base types".
2 Use typedef defined type alias in class
2.1 How to use
In the class, you can also use the type alias of int defined in "1 typedef defines type alias" MYINT.
class MyClass { MYINT i; };
At this time, the type of the member variable i of the custom class MyClass is int.
2.2 Notes
The type alias MYINT can be repeatedly defined in the class.
typedef int MYINT; class MyClass { MYINT i; typedef double MYINT; MYINT j; };
At this time, the type of member variable i of the custom class MyClass is int, and the type of member variable j is double.
Summary of type alias
Array(This content is excerpted from "C++ Primer Fifth Edition" P205)
typedef int arrt[10]; //arrt is a type alias. //It represents an array with 10 integersusing arrt=int[10]; //Arr's equivalent statementarrt* func(int i); //funcReturn a pointer containing10Pointer to array of integers
Function pointer parameter(This content is excerpted from "C++ Primer Fifth Edition" P222)
/* *Explanatory definition of formal parameters as pointers to functions */ void useBigger(const string &s1, const string &s2, bool (*pf)(const string &,const string &)); bool lengthCompare(const string &, const string &);
As the declaration statement of useBigger shows, using the function pointer type as a formal parameter directly seems lengthy and cumbersome. Type alias and decltype allow us to simplify the code using function pointers:
//Func and Func2 are function typestypedef bool Func(const string &, const string &); typedef decltype(lengthCompare) Func2; //Equivalent type//FuncP and FuncP2 are pointers to functionstypedef bool(*Funcp)(const string &, const string &); typedef decltype(lengthCompare) *FuncP2; //Equivalent type
We use typedef to define our own types. Func and Func2 are function types, while FuncP and FuncP2 are pointer types. It should be noted that decltype returns the function type, and the function type will not be automatically converted to a pointer type at this time. Because the result of decltype is a function type, you can only get a pointer by adding * before the result. You can redeclare useBigger in the following form:
//useBigger's equivalent declaration, where type alias is usedvoid useBigger(const string &, const string &, Func); void useBigger(const string &, const string &, FuncP2);
These two declaration statements declare the same function. In the first statement, the compiler automatically converts the function type represented by Func into a pointer.
Returns a pointer to a function
Similar to arrays, although it cannot return a function, it can return a pointer to the function type. However, we must write the return type as a pointer, and the compiler will not treat the function return type as the corresponding pointer type. As always, the easiest way to declare a function that returns a function pointer is to use a type alias:
using F = int(int*, int); //F is the return type, not a pointerusing PF = int(*)(int*, int); //PFIt's the pointer type
We use type alias to define F as a function type and PF as a pointer to the function type. It must be noted that the formal parameters of the function type are different, and the return type will not be automatically converted into a pointer. We must explicitly specify the return type as a pointer:
PF f1(int); //Correct: PF is a pointer to a function, f1 returns a pointer to a functionF f1(int); //Error: F is function type, f1 cannot return a functionF *f1(int); //correct:Explicitly specify that the return type is a pointer to the function
The above is personal experience. I hope you can give you a reference and I hope you can support me more.