简体   繁体   English

我无法理解c ++中的结构声明

[英]I can't understand the structure declaration in c++

In C, we have to use a struct prefix whenever we want to declare or define a structure. 在C中,每当我们想要声明或定义结构时,我们必须使用结构前缀。 However, things have been changed once the structure became a kind of a class in c++. 但是,一旦结构成为c ++中的一类,事情就会发生变化。 We no longer need to use a struct prefix when we declare a structure. 我们在声明结构时不再需要使用struct前缀。 In this vein, I guess the structure tag in C became a name of a type in C++ . 在这种情况下,我猜C的结构标记成为C++类型的名称。

However, it does not mean that we can't use a struct prefix. 但是,这并不意味着我们不能使用struct前缀。 We still can use a struct prefix. 我们仍然可以使用struct前缀。 For example, Bjarne Stroustrup, the creator of c++, introduces an example of declaring a structure both with and without a struct prefix, which makes me puzzled. 例如,c ++的创建者Bjarne Stroustrup引入了一个声明带有和不带struct前缀的结构的例子,这让我感到困惑。

Below are structure definitions which try to make a structure with template argument T. These compile fine with no error. 下面是尝试使用模板参数T创建结构的结构定义。这些编译正常,没有错误。

template<class T> struct linked_list {
    T element;
    linked_list<T> *next;
};
template<class T> struct linked_list {
    T element;
    struct linked_list<T> *next;
};

Now, below are function declarations whose return type and argument type are structures. 现在,下面是函数声明,其返回类型和参数类型是结构。 Even though these are not that different from the above, the first one from below two function declarations, the one with a struct prefix, gives me an error with Visual Studio c++ 2012 即使这些与上面没有什么不同,第一个从下面的两个函数声明,一个带结构前缀,给我一个错误的Visual Studio c ++ 2012

template<class T> struct linked_list<T> *add_list(T element, struct linked_list<T> *tail);
template<class T> linked_list<T> *add_list(T element, linked_list<T> *tail);

I really don't understand how things work. 我真的不明白事情是如何运作的。 I don't understand the differences between these declarations. 我不明白这些声明之间的区别。 Could anyone give me a detailed explanation? 谁能给我一个详细的解释?

Other than in C, in C++ the struct (and class ) keyword may be omitted, if there is no ambuiguity. 除了在C中,在C ++中,如果没有模糊性,可以省略struct (和class )关键字。 If there is ambiguity, you still have to use the struct keyword. 如果存在歧义,您仍然必须使用struct关键字。 A notorious example is POSIX' stat : there is a struct stat and a function stat . 一个臭名昭着的例子是POSIX的stat :有一个struct stat和一个函数stat Here you always have to use struct stat to refer to the type. 在这里,您始终必须使用struct stat来引用类型。

You do seem to understand pretty well as you explain it yourself. 当你自己解释时,你似乎很清楚。 In C++ the keyword struct is the same as the keyword class but with a default of public rather than private members. 在C ++中,关键字struct与关键字class相同,但默认为public而不是private成员。 So having declared a class using the struct keyword you do not then use it again when referring to that class. 因此,使用struct关键字声明了一个类,然后在引用该类时不再使用它。 You seem to be trying use struct as it would be used in C in the first example. 您似乎正在尝试使用struct,因为它将在第一个示例中用于C语言。 This is just different for C++. 这与C ++不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM