Question to follow up on this question to clarify the answer
1 answer
To answer this question, you should first refer to the C language standard, where the concept of anonymous structures and anonymous unions was introduced.
In C 2011, anonymous structures and anonymous unions are defined as follows (6.7.2.1 Structure and union specifiers)
13 Anonymous structure ; anonymous union . The structure of the unanimous body of anonymous government is considered. This applies recursively if you have anonymous.
As can be seen from this definition, not every nameless structure or nameless association is anonymous. Only unnamed structures and unnamed associations that are members of other structures or associations are anonymous.
In C ++, there are no anonymous classes, that is, classes declared with the keyword struct or class , but there are anonymous unions that are defined as follows (Standard C ++, Section 9.5 Unions)
5 A union of the form
union { member-specification } ; is called an anonymous union ; it defines an object of unnamed type . The member-specification of an anonymous union members. [Note: Nested types and functions cannot be declared union anonymous union. Anend note] It has been noted that the union has been declared. [Example:
void f() { union { int a; const char* p; }; a = 1; p = "Jennifer"; } Here, since the union has been registered, it’s the same as the address. —End example]
As can be seen from this definition, the concept of anonymous unions in C ++ is different from the corresponding definition of an anonymous unification in C. In C ++, you can declare an anonymous union outside of any structure or union.
Who is more interested in this issue, can also view the topic I have opened on isocpp.org
- Ie, in short, there are no anonymous classes in C ++. Right? - avp Nov.
- @avp Classes are defined with the class, struct and union keywords. In C ++, there are no anonymous classes declared with the class or struct keywords, but there are anonymous unions, that is, classes declared with the keyword union. - Vlad from Moscow
- @avp You can confidently say that in C ++ there are no anonymous structures. - Vlad from Moscow
- Interesting. Probably g ++ -std = c ++ 11 is not exactly C ++, since this is the structure
struct { int x, y; struct { int a, b; } p; } p;struct { int x, y; struct { int a, b; } p; } p;it fully accepts (as well as gcc (which is quite expected)) - avp - four@avp I'll wait for an answer to isocpp.org, then I'll let you know. :) - Vlad from Moscow