My lecturer just astounded me by insisting that the following is a valid C++ statement:
我的讲师只是坚持以下是一个有效的C ++语句让我震惊:
struct charlistrecord *next
The complete condition was found in declaring a recursive datatype in C/C++ as follows:
在C / C ++中声明递归数据类型时发现了完整的条件,如下所示:
typedef struct charlistrecord
{
char data;
struct charlistrecord *next;
} *charlist;
I want to know whether maybe there has been a time when this was accepted syntax or whether I've just been dumb for months of thinking I know C++.
我想知道是否有一段时间这是被接受的语法,或者我是否只是因为我认识C ++而愚蠢几个月。
4 个解决方案
#1
2
Prior to C++ and the assumption of typedefs, the only way to get a typed structure was via this syntax, and it was the only way to get a self-type-linked node.
在使用C ++和假定typedef之前,获取类型结构的唯一方法是通过这种语法,这是获得自键型链接节点的唯一方法。
struct Foo
{
int val;
struct Foo *link;
};
In C, this required any usage of Foo to be:
在C中,这需要Foo的任何使用:
struct Foo foo;
struct Foo *foo_ptr;
etc..
A typedef helped for this by doing this:
一个typedef通过这样做帮助了这个:
typedef struct Foo
{
int val;
struct Foo *link;
} Foo;
Since now you could do this:
从现在开始你可以这样做:
Foo foo; // same as struct Foo
Foo *foo_ptr; // same as struct Foo *
Note: Using a typedef to alias a struct Name
is not restricted to Name
as the alias. Yo were perfectly valid to do this as well:
注意:使用typedef为结构名称别名不限于Name作为别名。哟这样做完全有效:
typedef struct Foo
{
int val;
struct Foo *link;
} Bar;
and now the following are doable;
现在以下是可行的;
struct Foo foo;
Bar bar;
struct Foo *fooptr = &bar;
Bar *barptr = &foo;
Really makes you wish you programmed in C back in the day, doesn't it ? Probably doesn't clear things up, but hopefully a little less gray.
真的让你希望你在C当天回来编程,不是吗?可能不会澄清事情,但希望少一点灰色。
#2
1
It's a hangover from C. In C this is how you have to declare struct variables and members, so it's legal in C++ for reasons of backwards compatibility. Sounds like your lecturer is a bit 'old school'.
这是C的宿醉。在C中,这是你必须声明struct变量和成员的方式,所以它在C ++中是合法的,因为它们具有向后兼容性。听起来你的讲师有点“老派”。
#3
1
Yes it's totally valid. The word struct
here just explicitly states that next
is a pointer to a structure type named charlistrecord
.
是的,它完全有效。这里的struct这个词只是明确地声明next是指向名为charlistrecord的结构类型的指针。
It's a reminiscence from C, and you might as well omit the keyword in C++, but if you want to use it, you can.
这是C的回忆,你也可以省略C ++中的关键字,但是如果你想使用它,你可以。
#4
0
It's perfectly valid, and it can even be useful if your struct's name is shadowed by a variable, like here:
它是完全有效的,如果您的struct的名称被变量遮蔽,它甚至可能是有用的,如下所示:
struct C {};
int C = 0;
int a;
void foo() {
C *a; // multiplies int C by int a
struct C *a; //defines 'a' as pointer to struct C
}
#1
2
Prior to C++ and the assumption of typedefs, the only way to get a typed structure was via this syntax, and it was the only way to get a self-type-linked node.
在使用C ++和假定typedef之前,获取类型结构的唯一方法是通过这种语法,这是获得自键型链接节点的唯一方法。
struct Foo
{
int val;
struct Foo *link;
};
In C, this required any usage of Foo to be:
在C中,这需要Foo的任何使用:
struct Foo foo;
struct Foo *foo_ptr;
etc..
A typedef helped for this by doing this:
一个typedef通过这样做帮助了这个:
typedef struct Foo
{
int val;
struct Foo *link;
} Foo;
Since now you could do this:
从现在开始你可以这样做:
Foo foo; // same as struct Foo
Foo *foo_ptr; // same as struct Foo *
Note: Using a typedef to alias a struct Name
is not restricted to Name
as the alias. Yo were perfectly valid to do this as well:
注意:使用typedef为结构名称别名不限于Name作为别名。哟这样做完全有效:
typedef struct Foo
{
int val;
struct Foo *link;
} Bar;
and now the following are doable;
现在以下是可行的;
struct Foo foo;
Bar bar;
struct Foo *fooptr = &bar;
Bar *barptr = &foo;
Really makes you wish you programmed in C back in the day, doesn't it ? Probably doesn't clear things up, but hopefully a little less gray.
真的让你希望你在C当天回来编程,不是吗?可能不会澄清事情,但希望少一点灰色。
#2
1
It's a hangover from C. In C this is how you have to declare struct variables and members, so it's legal in C++ for reasons of backwards compatibility. Sounds like your lecturer is a bit 'old school'.
这是C的宿醉。在C中,这是你必须声明struct变量和成员的方式,所以它在C ++中是合法的,因为它们具有向后兼容性。听起来你的讲师有点“老派”。
#3
1
Yes it's totally valid. The word struct
here just explicitly states that next
is a pointer to a structure type named charlistrecord
.
是的,它完全有效。这里的struct这个词只是明确地声明next是指向名为charlistrecord的结构类型的指针。
It's a reminiscence from C, and you might as well omit the keyword in C++, but if you want to use it, you can.
这是C的回忆,你也可以省略C ++中的关键字,但是如果你想使用它,你可以。
#4
0
It's perfectly valid, and it can even be useful if your struct's name is shadowed by a variable, like here:
它是完全有效的,如果您的struct的名称被变量遮蔽,它甚至可能是有用的,如下所示:
struct C {};
int C = 0;
int a;
void foo() {
C *a; // multiplies int C by int a
struct C *a; //defines 'a' as pointer to struct C
}