C语言和C++中的struct 和typedef struct

C语言 中

// c
typedef struct Student {
   
    
    
    int age; 
} S;
等价于
// c
struct Student {
   
    
     
    int age; 
};

typedef struct Student S;
此时 S 等价于 struct Student,但两个标识符名称空间不相同。
另外还可以定义与 struct Student 不冲突的 void Student() {}。
C++ 中
由于编译器定位符号的规则(搜索规则)改变,导致不同于C语言。
一、如果在类标识符空间定义了 struct Student {…};,使用 Student me; 时,编译器将搜索全局标识符表,Student 未找到,则在类标识符内搜索。
即表现为可以使用 Student 也可以使用 struct Student,如下:

// cpp
struct Student 

猜你喜欢

转载自blog.csdn.net/it_xiangqiang/article/details/112977975