开发者

Specifying struct in function signature

Say I have

struct mystruct
{
};

Is there a difference between:

void foo(struct mystruct x){}

and

void foo(mys开发者_JS百科truct x){}

?


In C the latter isn't valid.

However in C++ they're almost the same: The first one would be valid if you haven't yet declared your struct at all, it would treat it as a forward declaration of the parameter all in one.


Not in the code you've written. The only difference I know of between using a defined class name with and without struct is the following:

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, don't define a function with the same name as a class.


No difference. The latter is the correct C++ syntax; the former is permissible as a legacy variant for recovering C programmers.

Note that struct and class are essentially the same and both define a class, so there's no special treatment for C-style POD structs in C++.

[Edit: Apparently there is a small difference, see Mark B's excellent answer.]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜