C++学习笔记(2)—类型定义typedef

版权声明:本博客主要记录学习笔记和遇到的一些问题解决方案,转载请注明出处! https://blog.csdn.net/u010982507/article/details/81142567

使用

1、typedef用来定义类型的同义词,例如:typedef int myint; myint age = 10;
2、typedef的语法格式:typedef 数据类型 标识符

使用typedef的目的

1、隐藏特定类型的实现,强调使用类型的目的;
2、简化复杂的类型定义,使其更容易理解;
3、允许一种类型用于多个目的,同时使得每次使用该类型的目的比较明确。

实例

#include <iostream>
#include <string>
using namespace std;

// 定义一个点的结构体
typedef struct MyStruct{
    int x;
    int y;
} Point;

typedef class Person {
public:
    int pid;
} Customer;

int main() {

    // 定义内置类型
    typedef int myInt;
    myInt age = 10;
    myInt a = 6;

    // 定义struct
    Point p;
    p.x = 1;
    p.y = 2;
    cout << p.x << "," << p.y << endl;

    // 定义类
    Customer c1;
    c1.pid = 1;
    cout << c1.pid << endl;

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u010982507/article/details/81142567
今日推荐