类成员名称中的下划线 [英] underscores in class member names

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

问题描述

我听说有些编译器保留''_''为系统启动符号

变量/符号。我无法在任何地方找到这些文件(到目前为止)。我已经在成员变量的末尾使用了一个''_''(即。Uint16 bar_;)来区分私有字段,但这并不代表如果

''_''在名称的开头就像下面的类一样

声明。


class Foo {

私人:

Uint16 _baz;

Uint32 _bar;


公开:

....

};

解决方案

BRIAN VICKERY写道:

我听说有些编译器保留''_''开始系统变量/符号的符号。


C ++语言保留了其中一些,例如:那些以

开头的下划线后跟一个大写字母和那些包含

两个连续下划线的那些。

我无法在任何地方找到这个记录(到目前为止)。
我在成员变量的末尾使用了''_''(即Uint16 bar_;)
来区分私有字段,


这是相当普遍的做法。

但是这并不像''_''在名称的开头那样突出以下课程
声明。

类Foo {
私人:
Uint16 _baz;
Uint32 _bar;

公开:
....
};




我认为那些应该没问题。




" BRIAN VICKERY" <是********* @ verizon.net>在消息中写道

新闻:LK ************** @nwrddc02.gnilink.net ...

我听说过一些编译器保留''_''为
系统变量/符号开始一个符号。我无法在任何地方找到这些文件(到目前为止)。
我在成员变量的末尾使用了一个''_''(即。Uint16 bar_;)来区分私有字段,但这并没有像''_''在名称的开头,如下面的类
声明。

类Foo {
私人:
Uint16 _baz; <公共:
....
};



来自17.4.3.1.2标准:


某些名称和功能签名总是保留给

实现:


- 每个包含双下划线(_ _)或以

下划线开头,后跟大写字母(2.11)的名称保留给

实现以供任何使用。 />

- 以下划线开头的每个名称都保留给实现

,用作全局命名空间中的名称。


BRIAN VICKERY在ne中写道ws:LK ************** @nwrddc02.gnilink.net:

我听说有些编译器保留''_' '开始系统变量/符号的符号。我无法在任何地方找到这个记录(所以
远)。我在成员变量的末尾使用了一个''_''(即。Uint16
bar_;)来区分私有字段,但这并不像
那样突出。 _''在名称的开头,如下面的类声明。

类Foo {
私人:
Uint16 _baz;
Uint32 _bar;

公开:
....
};




保留具有单个前导下限的标识符对于

实现如果:


其后跟一个大写字母(任何地方)。


其后是另一个下划线(任何地方),实际上*所有*标识符

*包含* 2或更多*连续*下划线被保留。


它在全球宣布命名空间范围,即它的一个宏或它

*是不是在命名空间,类,结构或联合中声明的它或者

*不是* a paramiter名称或本地变量名称。


也是IIUC它不应该是一个声明的外部C。


因此你对会员标识符的使用是好的。


虽然没有人真的喜欢它,但是大多数人都不会为了记住所有的规则而烦恼,所以他们可能会抱怨你使用了

保留的标识符,所以我会建议m_baz或其他一些东西。


Rob。

-
http://www.victim-prime.dsl.pipex.com/


I''ve heard that some compilers reserve the ''_'' starting a symbol for system
variables/symbols. I can not find this documented anywhere (so far). I''ve
used an ''_'' at the end of member variables (ie. Uint16 bar_; ) to
distinquish private fields, but this does not stand out as much as if the
''_'' were at the begginning of the name as in the following class
declaration.

class Foo {
private:
Uint16 _baz;
Uint32 _bar;

public:
....
};

解决方案

BRIAN VICKERY wrote:

I''ve heard that some compilers reserve the ''_'' starting a symbol for
system variables/symbols.
The C++ language reserves some of them, like e.g. those that begin with
an underscore followed by an uppercase letter and those that contain
two consecutive underscores.
I can not find this documented anywhere (so far).
I''ve used an ''_'' at the end of member variables (ie. Uint16 bar_; )
to distinquish private fields,
That''s quite common practise.
but this does not stand out as much as if the ''_'' were at the
begginning of the name as in the following class
declaration.

class Foo {
private:
Uint16 _baz;
Uint32 _bar;

public:
....
};



I think those shold be fine.



"BRIAN VICKERY" <is*********@verizon.net> wrote in message
news:LK**************@nwrddc02.gnilink.net...

I''ve heard that some compilers reserve the ''_'' starting a symbol for system variables/symbols. I can not find this documented anywhere (so far). I''ve used an ''_'' at the end of member variables (ie. Uint16 bar_; ) to
distinquish private fields, but this does not stand out as much as if the
''_'' were at the begginning of the name as in the following class
declaration.

class Foo {
private:
Uint16 _baz;
Uint32 _bar;

public:
....
};


From 17.4.3.1.2 of the Standard:

Certain sets of names and function signatures are always reserved to the
implementation:

- Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.

- Each name that begins with an underscore is reserved to the implementation
for use as a name in the global namespace.


BRIAN VICKERY wrote in news:LK**************@nwrddc02.gnilink.net:

I''ve heard that some compilers reserve the ''_'' starting a symbol for
system variables/symbols. I can not find this documented anywhere (so
far). I''ve used an ''_'' at the end of member variables (ie. Uint16
bar_; ) to distinquish private fields, but this does not stand out as
much as if the ''_'' were at the begginning of the name as in the
following class declaration.

class Foo {
private:
Uint16 _baz;
Uint32 _bar;

public:
....
};



Am identifier with single leading undersore is reserved for the
implementation if:

Its followed by an uppercase letter (anywhere).

Its followed by another underscore (anywhere), in fact *all* identifiers
that *contain* 2 or more *consecutive* underscores are reserved.

Its declared at global namespace scope, i.e its a macro or it
*isn''t* declared within an namespace, class, struct or union or it
*isn''t* a paramiter name or a local variable name.

Also IIUC it shouldn''t be a declared extern "C".

So you usage for member identifiers is Ok.

Nobody really likes it though and most people can''t be bothered to
remember all the rules, so they will probably complain that your using a
reserved identifier anyway, so I''d suggest m_baz or some such thing.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


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

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