构造函数实现分号后? [英] after constructor implementation semicolon?

查看:216
本文介绍了构造函数实现分号后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

当我需要实现我的类对象初始化时,我可以分几个阶段进行.我可以在构造函数主体中初始化:

Hello people!

When I need implement my class object initialization I can do it in several stages. I can initialize in constructors body:

...
anyClassConstructor::anyClassConstructor()
{
     anyMemberVariable = anyValue;
}
...



在初始化阶段:



Either in initialization stage:

...
anyClassConstructor::anyClassConstructor():anyMemberVariable(anyValue)
{
}
...



但是我看到,在初始化阶段实现构造函数初始化之后,最后出现了分号.



But I have seen, that after implement constructor initialization in initialization stage, there was semicolon in the end.

...
anyClassConstructor::anyClassConstructor():anyMemberVariable(anyValue)
{
}; // why there is semicolon?
...


据我所知,在类声明,成员方法声明(原型)或结束行之后需要分号.但是在类声明之后有分号,在构造函数实现后也有分号.为什么?有什么特别的东西吗?经过我的测试,它可以与分号一起使用,没有分号,是否没有错误,没有警告,没有错误?有效果吗?

有任何想法吗?谢谢...


As I know, semicolon is need after class declaration, member method declaration (prototypes) or to end line. But there is semicolon like after class declaration, and there is after constructor implement. Why? Is it any specific thing? After my test it''s work with semicolon and without it, no errors, no warnings, no bugs? Any effect?

Any ideas? Thanks...

推荐答案

不需要函数定义后的分号.那为什么编译器会容忍它呢?
您在此处看到的内容在C ++ 03中是不允许的,但是C ++ 11引入了所谓的空声明,请参阅:
http://en.wikipedia.org/wiki/C%2B%2B0x [ ^ ],
http://www.open-std.org/jtc1/sc22/wg21/ [ ^ ],
http://en.cppreference.com/ [ ^ ].

因此,在这种情况下,您观察到的内容将解释为空声明.显然,使用此分号会使代码的可移植性稍差一些.

请参阅: https://developer.mozilla.org/en/C++_Portability_Guide #Don%27t_put_extra_top-level_semi-colons_in_code [ ^ ].

但是,您不应该假定不需要的分号不会破坏代码.它可以.很明显,在很多情况下是不允许这样做的.为了您自己的利益,请只编写具有特定用途的代码元素,并且仅编写您完全理解的元素.

最好的祝福,
—SA
The semicolon after the function definition is not needed. So why your compiler tolerates it?
What you see here was not allowed in C++03, but C++11 introduced so called empty declaration, please see:
http://en.wikipedia.org/wiki/C%2B%2B0x[^],
http://www.open-std.org/jtc1/sc22/wg21/[^],
http://en.cppreference.com/[^].

So, what you observe in your case is interpreted as the empty declaration. Apparently, using this semicolon makes your code just a bit less portable.

Please see: https://developer.mozilla.org/en/C++_Portability_Guide#Don%27t_put_extra_top-level_semi-colons_in_code[^].

However, you should not assume an unwanted semicolon cannot spoil the code. It can. There is a number of cases where it is not allowed, quite apparently. For your own good, write only the elements of the code which have some purpose and only those you perfectly understand.

Best wishes,
—SA


这篇关于构造函数实现分号后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆