结构体:struct 和 typedef struct应该以及结构体的构造函数 区别

来源:互联网 发布:使命召唤ol优化差 编辑:程序博客网 时间:2024/05/16 06:37

主要记录 struct 和 typedef struct的笔记

参考博文
解释struct 构造函数的博文

# include <iostream>using namespace std;struct Student{    int age;}stu1;int main(){    stu1.age = 25;    cout << stu1.age << endl;    system("pause");}
# include <iostream>using namespace std;typedef struct Student{    int age;}stu2;int main(){    stu2 L;    L.age = 25;    cout << L.age << endl;    system("pause");}

使用时可以直接访问stu1.a
但是stu2则必须先 stu2 s2;

结构体和类的区别

参考博文
结构体 构造函数的实现

# include <iostream>using namespace std;typedef struct Student{    Student()    {        age = 0;        high = 0;    };    Student(int age1, double high1)    {        age = age1;        high = high1;    }    int age;    double high;}stu1;int main(){    stu1 L;    cout << L.high << endl;    system("pause");}
阅读全文
0 0
原创粉丝点击