在以前学c语言时候用struct时是这样的:
#include <stdio.h>
#include <stdlib.h>
struct node{
int a;
};
int main()
{
node b;
return 0;
}
c语言规定要用struct node b;来声明一个对象,或者如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int a;
}node_;
int main()
{
node_ b;
return 0;
}
此时node就相当于区别不同结构体的一个标识符没有实际意义,当然c语言中如下写也行:
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int a;
}node_;
int main()
{
node_ b;
return 0;
}
而在c++里面想怎么定义都行如下代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct node{
int a;
};
typedef struct{
int a;
}p;
typedef struct f{
int a;
};
typedef struct x{
int a;
}g;
int main()
{
node b;
p c;
f u;
g s;
x l;
return 0;
}
当然我试这么多c++写法的原因只是想说c++更方便了,为了保证代码可读性最好还是按照typedef struct node 或者 struct node这种写法