44.结构体-类型别名
44.1 知识点
什么是自定义数据类型
其实我们之前学习的枚举、结构体都可以称为自定义数据类型,但是我们这节课要学的自定义数据类型主要是指为原有数据类型定义一个别名。利用 typedef
关键字和 using
关键字可以帮助我们为数据类型定义别名。
typedef的使用
语法格式
typedef 原变量类型 新类型名;
基础变量类型别名
typedef int myInt;
typedef bool myBool;
typedef char myChar;
数组、指针、引用、结构体、枚举别名
typedef int myIntArray[10];
typedef int* myIntPtr;
typedef int& myIntRef;
typedef struct Person
{
int age;
bool sex;
} Person1, Person2;
typedef Person Person3;
enum E_Test
{
};
typedef E_Test E_Test2;
使用别名类型
myInt i = 1;
myBool b = true;
myChar c = 'A';
int i2 = 1; // 原来的变量类型也还是可以用的
myIntArray arr = { 1, 2, 3, 4, 5 };
myIntPtr ptr = &i;
Person1 p1 = { 1, true };
Person2 p2 = { 2, false };
Person3 p3 = { 2, false };
using的使用
语法格式
using 新类型名 = 原变量类型;
使用示例
using IntInt2 = int;
using IntArray2 = int[10];
using IntPtr2 = int*;
IntInt2 ii = 10;
别名的作用
之后使用有 int
和 double
参数无返回值的函数指针时,可以直接用 FuncPtr
作为别名,而不需要写一大坨类型定义。
typedef void (*FuncPtr1)(int, double);
using FuncPtr2 = void (*)(int, double);
void test(int a, double d)
{
std::string result = "a:" + std::to_string(a) + " d:" + std::to_string(d);
std::cout << result << std::endl;
}
void (*pp)(int, double) = test;
pp(1, 0.1); // a:1 d: 0.100000
FuncPtr1 pp1 = test;
pp1(2, 0.2); // a : 2 d : 0.200000
FuncPtr2 pp2 = test;
pp2(3, 0.3); // a: 3 d : 0.300000
别名的作用主要是代替复杂的、不方便书写和记忆的基本类型,提高代码的可读性。例如,可以用别名代表一个复杂的函数指针:
typedef 返回值 (*别名)(参数类型, 参数类型, .....);
using 别名 = 返回值 (*)(参数类型, 参数类型, .....);
44.2 知识点代码
Lesson44_类型别名.cpp
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
#pragma region 知识点二 typedef的使用
//语法格式
//typedef 原变量类型 新类型名;
//基础变量类型别名
typedef int myInt;
typedef bool myBool;
typedef char myChar;
//数组 指针 引用 结构体 枚举别名
typedef int myIntArray[10];
typedef int* myIntPtr;
typedef int& myIntRef;
typedef struct Person
{
int age;
bool sex;
}Person1, Person2;
typedef Person Person3;
enum E_Test
{
};
typedef E_Test E_Test2;
#pragma endregion
#pragma region 知识点三 using的使用
//语法格式
//using 新类型名 = 原变量类型;
using IntInt2 = int;
using IntArray2 = int[10];
using IntPtr2 = int*;
#pragma endregion
#pragma region 知识点四 别名的作用
//之后使用有int和double参数无返回值函数指针直接FuncPtr用FuncPtr1或FuncPtr2即可 不用写这么一大坨了
typedef void (*FuncPtr1)(int, double);
using FuncPtr2 = void (*)(int, double);
void test(int a, double d);
#pragma endregion
int main()
{
std::cout << "类型别名\n";
#pragma region 知识点一 什么是自定义数据类型
//其实我们之前学习的枚举、结构体都可以称为自定义数据类型
//但是我们这节课要学的自定义数据类型主要是指
//为原有数据类型定义一个别名
//利用
//typedef关键字 和 using关键字
//可以帮助我们为数据类型定义别名
#pragma endregion
#pragma region 知识点二 typedef的使用
//语法格式
//typedef 原变量类型 新类型名;
//使用别名类型
myInt i = 1;
myBool b = true;
myChar c = 'A';
int i2 = 1;//原来的变量类型也还是可以用的
myIntArray arr = { 1,2,3,4,5 };
myIntPtr ptr = &i;
Person1 p1 = { 1, true };
Person2 p2 = {2, false};
Person3 p3 = {2, false};
#pragma endregion
#pragma region 知识点三 using的使用
//语法格式
//using 新类型名 = 原变量类型;
IntInt2 ii = 10;
#pragma endregion
#pragma region 知识点四 别名的作用
//代替复杂的、不方便书写和记忆的基本类型,提高高代码的可读性
//比如:用别名代表一个复杂的函数指针
//typedef 返回值 (*别名)(参数类型, 参数类型, .....);
//using 别名 = 返回值 (*)(参数类型, 参数类型, .....);
void (*pp)(int, double) = test;
pp(1, 0.1);//a:1 d : 0.100000
FuncPtr1 pp1 = test;
pp1(2, 0.2);//a : 2 d : 0.200000
FuncPtr2 pp2 = test;
pp2(3, 0.3);//a: 3 d : 0.300000
#pragma endregion
}
#pragma region 知识点四 别名的作用
void test(int a, double d)
{
std::string result = "a:" + std::to_string(a) + " d:" + std::to_string(d);
std::cout << result << std::endl;
}
#pragma endregion
44.3 练习题
以下代码定义了哪个类型的别名?
typedef char* String;
为 char*
定义了别名为 String
请分别用 using
和 typedef
为 float
引用定义别名,注意是引用
using floatRef = float&;
typedef float& floatRef;
请分别用 using
和 typedef
为一个返回值为 int
,有两个 int
参数的函数定义函数指针别名
using funcPtr = int (*)(int, int);
typedef int (*funcPtr)(int, int);
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com