C++ C/C+中的冗余命名+;typedefs/structs #包括 #包括 常数int NAMELEN=30; 常量int MAXCLASSSIZE=10; 类型定义结构StudentRec{ 字符lastname[NAMELEN]; char firstname[NAMELEN]; 长int-ID; 国际终审法院; }学生;

C++ C/C+中的冗余命名+;typedefs/structs #包括 #包括 常数int NAMELEN=30; 常量int MAXCLASSSIZE=10; 类型定义结构StudentRec{ 字符lastname[NAMELEN]; char firstname[NAMELEN]; 长int-ID; 国际终审法院; }学生;,c++,c,C++,C,我对编码还不熟悉。我有一个问题,为什么会有学生;在括号后面。。这是我们必须遵循的格式。Student是typedef创建的新类型的名称。StudentRec是它定义的结构的名称。这将创建一个typename“Student”来表示结构。为此,使用 Type Fux/Cuth>在C++中是不必要的。可以这样定义: #include <stdio.h> #include <string.h> const int NAMELEN=30; const int MAXCLASS

我对编码还不熟悉。我有一个问题,为什么会有学生;在括号后面。。这是我们必须遵循的格式。

Student是typedef创建的新类型的名称。StudentRec是它定义的结构的名称。

这将创建一个typename“Student”来表示结构。为此,使用<代码> Type Fux/Cuth>在C++中是不必要的。可以这样定义:

#include <stdio.h>
#include <string.h>

const int NAMELEN=30;

const int MAXCLASSSIZE=10;

typedef struct StudentRec {
    char lastname[NAMELEN];
    char firstname[NAMELEN];
    long int ID;
    int finalmark;
}Student;
struct Student {
    char lastname[NAMELEN];
    char firstname[NAMELEN];
    long int ID;
    int finalmark;
};
struct foo myFoo;

你混淆了两件事。在C中,可以定义如下结构:

#include <stdio.h>
#include <string.h>

const int NAMELEN=30;

const int MAXCLASSSIZE=10;

typedef struct StudentRec {
    char lastname[NAMELEN];
    char firstname[NAMELEN];
    long int ID;
    int finalmark;
}Student;
struct Student {
    char lastname[NAMELEN];
    char firstname[NAMELEN];
    long int ID;
    int finalmark;
};
struct foo myFoo;
然后要使用一个,您必须执行以下操作:

struct foo {
    int a, b;
};
这真的很冗长,所以他们有一个很好的想法,使用typedef来制作一个新的类型。我可以这样做:

#include <stdio.h>
#include <string.h>

const int NAMELEN=30;

const int MAXCLASSSIZE=10;

typedef struct StudentRec {
    char lastname[NAMELEN];
    char firstname[NAMELEN];
    long int ID;
    int finalmark;
}Student;
struct Student {
    char lastname[NAMELEN];
    char firstname[NAMELEN];
    long int ID;
    int finalmark;
};
struct foo myFoo;
然后使用它:

typedef int myInt;
所以您要做的是声明有一个新类型,Student,它相当于struct StudentRec。按照惯例,许多人对typedef和struct使用相同的名称-这是合法的:

myInt x;

所有这些都不适用于
C++
。在
C++
中,首选:

typedef struct foo { int a, b; } foo;

其余部分仅适用于
C

技术上
structfoo{}对于大多数目的来说都足够了。但是,每次使用Foo类型时,都必须重复使用as
struct
,这有点冗长

struct Foo
{
   . . .
};
typedef使这更简单

struct Foo {}

void func( struct Foo* foo );
这可以通过以下方式进一步缩短:

struct Foo { };
typedef struct Foo Foo;
void func( Foo* foo );
执行单行程序时,也可以使用以下语法:

typedef struct Foo { } Foo;
void func( Foo* foo );
这是创建一个匿名
结构
,然后将其命名为Foo。这在
enum
s中最常见

typedef struct { } Foo;
void func( Foo* foo );
最初多余的Foo通常留在那里是有原因的。这是创建自引用结构(如链表)的唯一方法

typedef enum { } Bar;
如果ommited的初始
节点
,则无法声明指向该结构的指针。从技术上讲,这也是有效的,但现在你必须想出两个名字,而不是一个

typedef struct Node
{
   Node* next;
} Node;
那么为什么要使用:

typedef struct node_
{
   node_* next;
} Node;

为了统一。这种声明风格涵盖了所有的基础,因此在声明结构时不必考虑它。

在C中,结构名称和类型名称(如int)有不同的名称空间{类型名称空间与变量和函数名称空间共享,因此请注意}。定义新结构时,会自动将其名称添加到结构命名空间中,但当声明该类型的变量时,必须在其名称前面加上
struct
,以便编译器知道在结构命名空间中查找该变量的确切含义

使用typedef关键字可以将变量的名称添加到类型命名空间中,这样就不必在声明中使用struct关键字

您给出的示例将typedef声明与结构定义相结合,但在类型命名空间和结构命名空间中对结构使用了不同的名称。你可以这样做有两个原因。首先,用typedef作为一个看起来完全像变量声明的语句的前缀,定义了一个typename,而不是一个指定名称的变量。其次,可以通过在结构声明和分号之后立即包含其名称来声明结构类型的变量。你的例子结合了这些

C中还有其他法律形式。例如:

typedef struct Foo { } Foo;
C++有不同的名称空间结构,我相信您已经注意到了,因为它有namespace关键字。在这种情况下,尽管C++比C更简单,但定义一个结构(或类或联合或枚举)会自动将您使用的名称(如果有的话)添加到TyPulf添加名称的命名空间中。这在很大程度上大大简化了事情,但也存在一些问题。这些主要与保持与C的向后兼容性有关。这种兼容性主要与注意某人
typedef的struct foo foo,而不是将其视为尝试使用已使用的名称命名的错误

另一个问题是这种相当常见的C代码:

/* This declares a variable foo whose type has no name but is a struct that contains one int named x */
struct {
    int x;
} foo;

/* This adds bar to the type namespace without adding an entry to the struct namespace. */
typedef struct {
    int y;
} bar;

/* This adds the name baz to the struct namespace and declares a variable named b of this type */
struct baz {
    int z;
} b;

/* This adds the name ralf to the type namespace which also refers to this type */
typedef struct baz ralf;

这在C语言中很常见,尤其是当一个结构中只有一个需要存在时。在C中,这很容易,因为
struct shoe
的名称与变量
shoe
之间不会发生冲突。在C++中,这仍然可以保持与C

的向后兼容性,即C,而不是C++。你确定你指的是正确的语言吗?C或C++,在这个例子中没有区别。在谷歌上或在你的书中查找TyEnFIFF。@布兰特:是的,但是没有理由在C++中声明这样的结构。@ BelLyOnA:正确,但它仍然是有效的C++。我看到这做了很多,大概是由继承了C语言习惯的人做的。你们是否不能阅读实际问题“这是我们必须遵循的格式吗?”在大多数情况下,这是不必要的,但结构的定义和类型定义之间有细微的差异。标识符驻留在不同的名称空间中(不是C++中的
名称空间
关键字)。