zoukankan      html  css  js  c++  java
  • struct 和 typedef struct

    分三块来讲述:
      1 首先://注意在C和C++里不同
        在C中定义一个结构体类型要用typedef:
        typedef struct Student
        {
        int a;
        }Stu;
        于是在声明变量的时候就可:Stu stu1;(如果没有typedef就必须用struct Student stu1;来声明)
        这里的Stu实际上就是struct Student的别名。Stu==struct Student
        另外这里也可以不写Student(于是也不能struct Student stu1;了,必须是Stu stu1;)
        typedef struct
        {
        int a;
        }Stu;
        但在c++里很简单,直接
        struct Student
        {
        int a;
        };    
        于是就定义了结构体类型Student,声明变量时直接Student stu2;

    2.其次:
        在c++中如果用typedef的话,又会造成区别:
        struct   Student  
        {  
        int   a;  
        }stu1;//stu1是一个变量 

              typedef   struct   Student2  
        {  
        int   a;  
        }stu2;//stu2是一个结构体类型=struct Student

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

    3 掌握上面两条就可以了,不过最后我们探讨个没多大关系的问题
        如果在c程序中我们写:
        typedef struct 
        {
        int num;
        int age;
        }aaa,bbb,ccc;
        这算什么呢?
        我个人观察编译器(VC6)的理解,这相当于
        typedef struct 
        {
        int num;
        int age;
        }aaa;
        typedef aaa bbb;
        typedef aaa ccc;
        也就是说aaa,bbb,ccc三者都是结构体类型。声明变量时用任何一个都可以,在c++中也是如此。但是你要注意的是这个在c++中如果写掉了typedef关键字,那么aaa,bbb,ccc将是截然不同的三个对象。

  • 相关阅读:
    mysql查找有某列但没有此列索引的表
    mysql找到所有索引
    mysq在某一刻同时获取主从库的位置点
    新书《深入应用C++11:代码优化与工程级应用》出版,感谢支持
    c++11实现一个简单的lexical_cast
    应该用bind+function取代虚函数吗?
    《深入应用C++11:代码优化与工程级应用》开始发售
    一个更好的C++序列化/反序列化库Kapok
    C++技术沙龙主要内容
    C++11模版元编程
  • 原文地址:https://www.cnblogs.com/mbcxm/p/3680988.html
Copyright © 2011-2022 走看看