7

In C, we have to use a struct prefix whenever we want to declare or define a structure. However, things have been changed once the structure became a kind of a class in c++. We no longer need to use a struct prefix when we declare a structure. In this vein, I guess the structure tag in C became a name of a type in C++.

However, it does not mean that we can't use a struct prefix. We still can use a struct prefix. 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.

Below are structure definitions which try to make a structure with template argument T. These compile fine with no error.

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

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?

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • 1
    Are you asking why you can omit the `struct` keyword in C++? [Why does C need “struct” keyword and not C++?](http://stackoverflow.com/questions/8422775/why-does-c-need-struct-keyword-and-not-c) – Blastfurnace Aug 05 '13 at 19:30
  • What compiler are you using? Not all C++ comply to the standards. – Anycorn Aug 05 '13 at 19:46
  • 1
    @Blastfurnace Thank you. However, i still have a question. The link that you gave me said if there is no ambiguity the c++ 'allows' the omission of the struct keyword. It means we can still use the struct keyword. It then follows that there is an ambiguity in my second codes because it gives me an error. However, I don't see any ambiguity. What's wrong? –  Aug 05 '13 at 19:46
  • @Anycorn I'm using the visual studio c++ 2012. –  Aug 05 '13 at 19:47
  • 1
    With g++, both lines compile. – Anycorn Aug 05 '13 at 19:49
  • @Anycorn Wow. Thank you for your help. I have an another question. The reason why a structure can have a template argument is because a structure is a kind of a class? I remember Bjarne Stroustrup saying that only a class and a function can have a template argument. –  Aug 05 '13 at 19:53
  • @bis0317: In C++ the only difference between a `struct` and a `class` is that `struct` members are public by default and `class` members are private by default. Other than the default member visibility, there is _no differences whatsoever_ – Mooing Duck Aug 05 '13 at 22:08
  • Also "As you already know, a structure is a class whose members are all public, which makes it possible that we no longer need to use a struct prefix when we declare a structure." Those two things are 100% completely unrelated, not cause and effect. – Mooing Duck Aug 05 '13 at 22:09
  • I've confirmed that neither MSVC '08 nor MSVC '12 will compile his code. – Mooing Duck Aug 05 '13 at 22:19

2 Answers2

3

Other than in C, in C++ the struct (and class) keyword may be omitted, if there is no ambuiguity. If there is ambiguity, you still have to use the struct keyword. A notorious example is POSIX' stat: there is a struct stat and a function stat. Here you always have to use struct stat to refer to the type.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
-1

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. So having declared a class using the struct keyword you do not then use it again when referring to that class. You seem to be trying use struct as it would be used in C in the first example. This is just different for C++.

David Elliman
  • 1,379
  • 8
  • 15
  • So, you're basically summed up his question. – jrok Aug 05 '13 at 19:29
  • However, It is possible that we use a struct prefix even after the definition of a structure. We can see this in my first code. It's so complicated. :-( –  Aug 05 '13 at 19:33
  • It's not really complicated it is just that C++ is a different language to C. The keyword struct means something different. Objective-C is an extension of C so all C remains valid, but Stroustrup decided not to maintain such downward compatibility when designing C++. – David Elliman Aug 05 '13 at 20:46
  • @DavidElliman: I just reread that and realized that though you're 100% correct, the backwards compatability doesn't explain anything about this question. C++ can compile most C code, and Objective-C can compile all C code. All three compile `struct linked_list`. – Mooing Duck Aug 06 '13 at 16:36