What is the difference between the two ads?

I only ask in the context of C ++.

struct x1 { ... }; typedef struct { ... } x2; int main() { x1 a; x2 b; } 
+6
source share
1 answer

The first defines a class called x1 .

The second defines an unnamed class, and also defines a type alias named x2 .

In C ++, the difference is very subtle. You can notice the difference when trying to declare a function with the same name:

 void x1(); // OK void x2(); // not OK, redefined as a different type of symbol 

In practice, you should avoid defining a function with the same name as the class inside the same namespace, so this difference almost never occurs. The first one is usually preferable because it is simpler.

In C, the difference affects the use of the identifier a bit more.

+4
source

All Articles