C ++成员变量 [英] C++ Member Variables

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

问题描述

请考虑以下课程:

class A
{
  A();
  int number;
  void setNumber(int number);
};

您可以通过3种方式实现'setNumber':

You could implement 'setNumber' in 3 ways:

方法1 :使用"this"指针.

Method 1: Use the 'this' pointer.

void A::setNumber(int number)
{
  this->number = number;
}

方法2 :使用范围解析运算符.

Method 2: Use the scope resolution operator.

void A::setNumber(int number)
{
  A::number = number;
}

方法3 :相反,用'm'或'_'表示所有成员变量(这是我的首选方法).

Method 3: Instead, denote all member variables with 'm' or '_' (this is my preferred method).

void A::setNumber(int number)
{
  mNumber = number;
}

这仅仅是个人喜好,还是选择一种特定的方法有好处?

Is this just personal preference, or is there a benefit to choosing a particular method?

推荐答案

这主要是个人喜好,但让我在同时制作许多小型游戏的公司内部分享我对该问题的看法(因此,我周围使用了许多编码样式.

This is mostly a personal preference, but let me share my perspective on the issue from inside a company where many small games are being made simultaneously (and so there are many coding styles being used around me).

此链接具有几个相关的良好答案:为什么在C ++类中的成员变量上使用前缀

This link has several good, related, answers: Why use prefixes on member variables in C++ classes

您的选项1:

void A::setNumber(int number)
{
  this->number = number;
}

首先,许多程序员趋向于发现这一麻烦之处,不断地键入"this->".其次,更重要的是,如果您的任何变量与参数或局部变量共享名称,则查找替换设计为说,更改数字"的名称可能会影响位于查找替换范围内的成员变量,如下所示:好吧.

First off, many programmers tend to find this cumbersome, to continually type out the ' this-> '. Second, and more importantly, if any of your variables shares a name with a parameter or a local variable, a find-replace designed to say, change the name of 'number' might affect the member variables located in the find-replace scope as well.

您的选项2:

void A::setNumber(int number)
{
  A::number = number;
}

我遇到的问题是,在大型类或具有大型函数的类中(您看不到该函数或该类被意外命名),A::( thing)的格式看起来非常就像访问名称空间的一部分一样,因此可能会产生误导.另一个问题与上一个选项中的#2相同,如果您的名称与您正在使用的任何变量相似,则有时可能会出现意想不到的混乱.

The problem I've run into with this, is that in large classes, or classes with large functions (where you cannot see the function or the class is named unexpectedly), the formatting of A::(thing) looks very much like accessing a part of a namespace, and so can be misleading. The other issue is the same as #2 from the previous option, if your names are similar to any variables you're using there can be unexpected confusion sometimes.

您的选项3:

void A::setNumber(int number) 
{
  mNumber = number;
}

这是这三个选项中最好的.通过创建(并坚持使用)包含清晰有意义的前缀的语法,您不仅可以创建一个本地(或全局)变量不会共享的唯一名称,而且还可以立即明确声明该变量的声明位置,不论您在什么上下文中找到它.我都看到它既可以像'mVariable'一样,也可以像'm_variable'这样来完成,这主要取决于您是否喜欢使用下划线而不是大写并置.另外,如果您的样式倾向于添加诸如p代表指针的p或g代表全局变量的g的样式,则该样式将很好地结合在一起,并为读者所期望.

This is the best of those three presented options. By creating (and holding to!) a syntax that involves a clear and meaningful prefix, you not only create a unique name that a local (or global) variable won't share, but you make it immediately clear where that variable is declared, regardless of the context you find it in. I've seen it done both like this ' mVariable ' and like this 'm_variable' and it mostly depends upon if you prefer underscores to uppercase concatenation. In addition, if your style tends to add things like ' p 's on for pointers, or ' g 's on for globals, this style will mesh well and be expected by readers.

这篇关于C ++成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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