Possible Duplicate:
Difference between ‘struct’ and ‘typedef struct’ in C++?
An answer to this question led me to wonder about the following:
I presume that defining a class as follows:
typedef class {int i;} C;
would be completely equivalent to defining it in the conventional manner:
class C
{
int i;
};
Is this presumption correct?
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
structor aclassdeclared in this way, simply because theclassis unnamed. Similarly you cannot declare any function that involves the class’ name. Here are some examples:You also cannot forward declare an anonymous class:
In C++ I have never seen a case where
typedefing an anonymousstructor aclassis preferable to simply declaring aclassor astructthat is named. In some cases the traditional method is definitely preferred. The moral of the story is: don’t usetypedef class {} Name;in C++. It buys you nothing, and costs you something.