Is there any difference 开发者_高级运维between these:
struct Class* CreateClass();
and:
Class* CreateClass();
It's just a factory function declaration. You can see that one has struct at the start and one doesn't. I've tried it both ways and it doesn't seem to make a difference.
Which should I be using?
It's from C; there's no difference* in C++.
*Okay, I lied, sorry. :P You can confuse yourself if you really want to, and make them be different if you use a typedef
with the same name but a different underlying type, but normally they're not different. That's assuming Class
is already declared, though... if Class
is undeclared, the second one won't even compile.
That said, the convention is to do:
typedef struct Class { ... } Class;
so that it compiles the same way in both C and C++.
It doesn't make a difference as long as there's no function in scope with the same name Class
. You shouldn't write a function with the same name as a class, and to follow common C++ style you should use just Class *
.
[Edit: same correction as Mehrdad, no function or typedef]
In C++, there is no difference. The use of "struct
" there is just for backwards compatibility with C, in which declaring struct foo { ... };
would declare a type named "struct foo
", and "foo
" would only be a valid type name if you then used typedef struct foo foo
.
精彩评论