In this isolated example they are functionally the same, at least from the outside.
However there are differences. One instance in particular, you cannot declare a constructor for a struct
or a class
declared in this way, simply because the class
is unnamed. Similarly you cannot declare any function that involves the class' name. Here are some examples:
typedef class
{
public:
Gizmo() : n_(42) {}; // NOT OK
~Gizmo();
Gizmo& operator<<(int n);
private:
int n_;
} Gizmo;
You also cannot forward declare an anonymous class:
class Gizmo;
In C++ I have never seen a case where typedef
ing an anonymous struct
or a class
is preferable to simply declaring a class
or a struct
that is named. In some cases the traditional method is definitely preferred. The moral of the story is: don't use typedef class {} Name;
in C++. It buys you nothing, and costs you something.