Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ return type question

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?

like image 636
Ryan Avatar asked May 09 '11 01:05

Ryan


2 Answers

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++.

like image 100
user541686 Avatar answered Sep 19 '22 12:09

user541686


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]

like image 41
Steve Jessop Avatar answered Sep 20 '22 12:09

Steve Jessop