使用"struct"关键字进行struct实例化

Pau*_* S. 4 c++ struct declaration instantiation keyword

假设我声明了一个新的C++结构类型:

struct my_struct {
   int a;
   int b;
};
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式创建此结构类型的新实例:

my_struct foo;
Run Code Online (Sandbox Code Playgroud)

要么

struct my_struct foo;
Run Code Online (Sandbox Code Playgroud)

如果两者都有效,那有什么区别吗?

Ben*_*ley 14

是的,您可以使用任何一种方法.区别在于这种形式:

my_struct foo;
Run Code Online (Sandbox Code Playgroud)

在C中不合法,所以你必须使用这个表格:

struct my_struct foo;
Run Code Online (Sandbox Code Playgroud)

C++支持C语言向后兼容C语言.