为什么在C ++类中的成员变量上使用前缀 [英] Why use prefixes on member variables in C++ classes

查看:138
本文介绍了为什么在C ++类中的成员变量上使用前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多C ++代码使用语法约定来标记成员变量。常见示例包括

A lot of C++ code uses syntactical conventions for marking up member variables. Common examples include


  • m_ * memberName *用于公共成员(根本使用公用成员)

  • <对于私人成员或所有成员,

其他人尝试使用 - >

Others try to enforce using this->member whenever a member variable is used.

根据我的经验,大多数较大的代码库不能一致地应用这些规则。

In my experience, most larger code bases fail at applying such rules consistently.

在其他语言中,这些惯例远不如普遍。我看到它只是偶尔在Java或C#代码。我想我从来没有见过它在Ruby或Python代码。因此,似乎有一种趋势,更多的现代语言不使用成员变量的特殊标记。

In other languages, these conventions are far less widespread. I see it only occasionally in Java or C# code. I think I have never seen it in Ruby or Python code. Thus, there seems to be a trend with more modern languages to not use special markup for member variables.

这个约定今天在C ++中仍然有用,或者它只是一个时代错误。特别是因为它在图书馆之间如此不一致地使用。没有其他语言显示没有成员前缀?

Is this convention still useful today in C++ or is it just an anachronism. Especially as it is used so inconsistently across libraries. Haven't the other languages shown that one can do without member prefixes?

推荐答案

您必须小心使用前导下划线。保留字的大写字母前的下划线。
例如:

You have to be careful with using a leading underscore. A leading underscore before a capital letter in a word is reserved. For example:

_Foo

_L

都是保留字,而

_foo

_l

不是。还有其他情况,不允许使用小写字母前的下划线。在我的具体情况下,我发现_L恰好由Visual C ++ 2005保留,并且冲突创建了一些意想不到的结果。

are not. There are other situations where leading underscores before lowercase letters are not allowed. In my specific case, I found the _L happened to be reserved by Visual C++ 2005 and the clash created some unexpected results.

我在围绕它是多么有用以标记局部变量。

I am on the fence about how useful it is to mark up local variables.

这是一个关于标识符保留的链接:
http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an -underscore-in-ac-identifier

Here is a link about which identifiers are reserved: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier

这篇关于为什么在C ++类中的成员变量上使用前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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