应该用一个C ++类的函数结束。或只有一个}? [英] Shall a C++ class function end with a }; or only a }?

查看:239
本文介绍了应该用一个C ++类的函数结束。或只有一个}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关C ++语法的基本问题。



或者没关系吗?



或者这两个例子之间有什么区别?



  void Test :: print()
{
...
}

无;

 code> void Test :: print()
{
...
}

我看过有和没有
的C ++代码,但我没有区别。



/谢谢






更新



情况;

解决方案

在类体之外(其中函数定义可以可选地跟随单个; ),函数定义终止于复合语句是关闭} 。在命名空间范围,函数定义的结束大括号之后的下一个标记必须成为下一个声明的一部分。



在C ++ 03,没有这样的事情作为空声明所以放置一个; 是非法的,虽然被许多实现接受。 (尽管C ++ 03 简单声明的语法似乎允许缺少 decl-specifier-seq 和缺少 init-declarator-list ,只留下一个; ,有一个语义规则禁止两个可选部分在标准的7 [dcl.dcl] / 3中被省略。 / p>


在一个简单的声明中,可选的 init-declarator-list 或者枚举(7.2),即当 decl-specifier-seq 包含 class-specifier 类键(9.1)或枚举指示符


C ++ 0x引入了一个空声明(这是一个没有效果的声明),所以你可以拥有多少流氓; 在命名空间范围,你喜欢,虽然没有好的理由这样做。


A basic questions about C++ syntax.

Shall a c++ function end with a ; or does it not matter?

Or what is the difference between those two examples?

With a ending ;

void Test :: print()
{
  ...
};

Without ;

void Test :: print()
{
  ...
}

I have seen C++ code both with and without, but I don't get the difference.

/Thanks


Update:

In this case the ; should be avoided since it is not doing anything useful and may even become a problem in the future.

解决方案

Outside of a class body (where a function definition may optionally be followed by a single ;), a function definition is terminated by the end of a compound-statement - that is the closing }. At namespace scope, the next token after the closing brace of a function-definition must form part of the next declaration.

In C++03, there is no such thing as an empty-declaration so placing a ; is illegal, although accepted by many implementations. (Although the syntax of a C++03 simple-declaration appears to allow a missing decl-specifier-seq and a missing init-declarator-list, leaving just a ;, there is a semantic rule forbidding both optional parts to be left out expressed in 7 [dcl.dcl] / 3 of the standard.)

In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (clause 9) or enumeration (7.2), that is, when the decl-specifier-seq contains either a class-specifier, an elaborated-type-specifier with a class-key (9.1), or an enum-specifier.

C++0x introduces an empty-declaration (which is a declaration with no effect), so you can have as many rogue ; at namespace scope as you like although there is no good reason to do so.

这篇关于应该用一个C ++类的函数结束。或只有一个}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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