I am only asking in context of C++.
struct x1 { ... };
typedef struct { ... } x2;
int main()
{
x1 a;
x2 b;
}
The first defines a class with the name x1
.
The second defines an unnamed class and also defines a type alias, by the name x2
to it.
The difference is very subtle in C++. You can observe the difference by trying to declare a function by 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 by the same name as a class within the same namespace, so this difference hardly ever comes up. The first one is typically preferred because it's simpler.
In C, the difference affects the use of the identifier a bit more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With