没有名称的类 [英] Class with no name

查看:183
本文介绍了没有名称的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++标准文档中阅读了这个类:

I read this about class in the C++ standard document:


类是一个类型。它的名称在其
范围内成为类名(9.1)。

A class is a type. Its name becomes a class-name (9.1) within its scope.

class-name: identifier template-id


我发现C ++标准中的标识符的语法: / p>

I found this grammar for an identifier in the C++ Standard:

 2.10 Identifiers
 identifier: nondigit
 identifier nondigit
 identifier digit

 nondigit: one of universal-character-name 
 _ a b c d e f g h i j k l m n o p q r s t u  v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
 digit: one of 0 1 2 3 4 5 6 7 8 9

现在我尝试这样做:

class
{
public:
  int i;
};

它编译正常,没有任何名称。

and it compiles fine without any name.

任何人都可以给我一个解释?是不是违反了为标识符指定的语法?

Can anyone give me an explanation regarding this? Isn't it a violation of the grammar specified for an identifier?

Nawaz问了关于标准的后续问题遵守我给予的守则。有兴趣的人可以在这里查看。 p>

Nawaz had asked a follow up question regarding the standard compliance of the code I had given. Those interested can check it out here.

推荐答案

语法

class-specifier:
    class-head { member-specification_opt }

class-head:
    class-key attribute-specifier-seq_opt class-head-name class-virt-specifier-seq_opt base-clause_opt
    class-key attribute-specifier-seq_opt base-clause_opt

class-key:
    class
    struct
    union

在你的情况下,第二个 class-head - 不涉及 class-name

In your case, the second production of class-head is used -- no class-name is involved.

这篇关于没有名称的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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