Specifying a structure signature in a function

Say I have

struct mystruct
{
};

Is there a difference between:

void foo(struct mystruct x){}

and

void foo(mystruct x){}

?

+5
source share
3 answers

Not in the code you wrote. The only difference I know between using a specific class name with and without structis as follows:

struct mystruct
{
};

void mystruct() {}

void foo(struct mystruct x){} // compiles
void foo(mystruct x){} // doesn't - for compatibility with C "mystruct" means the function

So, do not define a function with the same name as the class.

+7
source

In C, the latter is invalid.

However, in C ++ they are almost the same: the first one will be valid, if you have not declared your structure at all, it will consider it as a direct declaration of an all-in-one parameter.

+11
source

. ++; C.

Note that structboth classare essentially the same and both define a class, so there is no special reference to COD COD structures in C ++.

[Edit: There seems to be a slight difference, see Mark B.'s excellent answer.]

+3
source

All Articles