本文介绍了使用与成员变量相同的参数名称是不好的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,是以下任何一种
- 不好的做法
- li>
- 无效率(调用
此
指针) - 任何其他原因
- Bad practice
- Unreadable
- Inefficient (the call to
this
pointer) - Any other reason why it's bad to do this
。
class Person {
public:
string name;
Person(string name) {
this->name = name;
}
};
PS
Person(string name):name(name){}
推荐答案
em> (不是真正的问题)我可以想到的是,你不能区分成员变量
与局部变量
或
函数参数
。它只是编码风格,它与效率无关,但是当你谈论 不可读
时,对我来说是对的。
The only issue(not a real issue) I can think of is that you can't distinguish member variable
with local variable
or function parameter
. It's just coding style, it's nothing to do with efficiency, but when you talk about Unreadable
, that's yes for me.
对我来说,我通常使用尾部下划线命名类成员变量。它有助于代码的可读性,并使维护更容易。
For me I normally name class member variable with trailing underscore. It helps code readability and makes it easier for maintenance.
class Person {
public:
string name_; // member variable with traling `_`
string m_surname; // some microsoft style declares member start with `m_`
Person(const string& name) // pass parameter by reference.
: name_(name) // you know you are constructing member name_ with name variable
{
}
};
这篇关于使用与成员变量相同的参数名称是不好的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!