C ++ type return question

Is there a difference between the two:

struct Class* CreateClass();

and

Class* CreateClass();

This is just a declaration of a factory function. You can see that in the beginning there is a structure, and the other is not. I tried this in both directions and it doesn't seem to matter.

What should i use?

+6
source share
3 answers

This is from C; there is no difference * in C ++.

* Ok, I lied, sorry .: P You can embarrass yourself if you want and make them different if you use typedef with the same name but with a different base type, but usually they are not different. Suppose Class already declared, though ... if Class not declared, the second one will not even compile.

However, the agreement should do:

 typedef struct Class { ... } Class; 

so it compiles in the same way in both C and C ++.

+6
source

This does not matter if the Class function does not exist in the region with the same name. You should not write a function with the same name as the class, and follow the general C ++ style, you should use only Class * .

[Edit: same correction as Mehrdad, no function or typedef]

+3
source

In C ++ there is no difference. The use of " struct " exists only for backward compatibility with C, in which the declaration struct foo { ... }; will declare a type named " struct foo ", and " foo " will only be a valid type name, if you then use typedef struct foo foo .

0
source

Source: https://habr.com/ru/post/887582/


All Articles