使用无前缀成员变量时如何命名构造函数参数?
当然,没有一种正确的方法可以做到这一点,但我什至想不出任何合适的命名方案,这就是我在这里问的原因。 (所以:虽然所有答案都是主观,但它们仍然有用!)
问题如下:对于简单的聚合结构,我们不使用成员var前缀。
struct Info {
int x;
string s;
size_t z;
Info()
: x(-1)
, s()
, z(0)
{ }
};
然而,有时提供一个初始化构造函数来初始化结构是有用的,但是,当成员变量本身已经采用了最自然的名称时,我无法为参数想出一个合适的命名方案:
struct Info {
int x;
string s;
size_t z;
Info(int x?, string s?, size_t z?)
: x(x?)
, s(s?)
, z(z?)
{ }
};
其他人是什么?在这种情况下使用?
Certainly there is no one right way to do this, but I can't even think of any decent naming scheme, that's why I'm asking here. (So: While all answers will be subjective, they will be useful nevertheless!)
The problem is as follows: For simple aggregate structs, we do not use member var prefixes.
struct Info {
int x;
string s;
size_t z;
Info()
: x(-1)
, s()
, z(0)
{ }
};
It is nevertheless sometimes useful to provide an initializer ctor to initialize the struct, however - I cannot come up with a decent naming scheme for the parameters when the most natural names for them are already taken up by the member variables themselves:
struct Info {
int x;
string s;
size_t z;
Info(int x?, string s?, size_t z?)
: x(x?)
, s(s?)
, z(z?)
{ }
};
What are other people using in this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
为什么要发明前置/后缀?我只是使用相同的名字。 C++ 就是为此而设计的。
很简单。
Why invent pre/postfixes? I just use the same name. C++ is designed for that to work.
It's straight forward.
使用相同的名称 - 它们不会冲突。
“在构造函数 (12.6.2) 的 mem-initializer 表达式中查找使用的名称时,函数参数名称为
可见并隐藏在包含函数声明的块、类或命名空间范围中声明的实体的名称。” 3.4.1 C++ 2003
Use the same names - they don't collide.
"During the lookup for a name used ... in the expression of a mem-initializer for a constructor (12.6.2), the function parameter names are
visible and hide the names of entities declared in the block, class or namespace scopes containing the function declaration." 3.4.1 C++ 2003
我倾向于使用“a”前缀——如“awhatever”。
I tend to use "a" prefix - as in "a whatever".
我在这里看到的是对构造函数参数使用尾随下划线,例如:
Something I've seen around here is using trailing underscores for constructor arguments, e.g.:
我正在使用这个:
这可能有点令人惊讶,但这是完全合法的。
另请参阅:
我可以对字段和构造函数使用相同的名称吗参数?
构造函数的参数命名
I am using this:
It may be a bit surprise, but this is perfectly legal.
See also:
Can I use identical names for fields and constructor parameters?
Parameters Naming for Constructor
我相信只要您使用初始化列表,就可以使用相同的名称:
如果您必须做一些工作来初始化字段,您仍然可以使用相同的名称,但会少一点方便的:
I believe as long as you're using the initialization list, you can use the same names:
If you had to do some work to initialize a field, you could still get away with using the same names, but it would be a little less convenient:
您可以使用与成员名称相同的参数名称(但有些人觉得这很混乱)。
我见过成员使用前缀/后缀(
_
、m
、my_
很流行,在这种情况下与参数没有冲突)或参数(根据我的经验,前缀p
是最常用的)。You can use parameter name identical to member names (but some find it confusing).
I've seen the use of prefix/suffix for members (
_
,m
,my_
are popular in which case there is no conflict with parameters) or for parameters (a prefix ofp
is the most popular for that use in my experience).如果只是将成员设置为相应参数的值,请使用初始值设定项列表。这里成员变量和参数使用相同的名称是绝对安全的。如果不能简单地赋值,而必须调用函数,那么让成员和参数的名称易于区分是至关重要的,同时也易于使关系可见,这里前缀或后缀是一个不错的选择。
Erik的回答建议使用前缀
a
,但我发现更改第一个字母需要额外的工作或将原始名称改为大写以使参数名称非常烦人,它会阻止简单地使用复制和粘贴(让它保持不变不是一个选项,因为您不想通过为每个单独的情况添加前缀来观察含义的潜在变化)。对于无法使用初始化列表的情况,我想出了
a_
,它可以用作原始名称之前的前缀。In cases where members are simply set to the values of the corresponding parameters, use the initializer list. Here using the same name for member variable and parameter is absolutely safe. If you cannot simply assign values, but have to call functions, it's vital to make the names of members and parameters easy to distinguish, but also easy to make the relation visible, here a prefix or postfix is a good option.
Erik's answer suggests the prefix
a
, but I find the extra-work to change the first letter or the original name to upper case for getting the parameter name extremely annoying, it prevents simply using copy&paste (Letting it unchanged is not an option because you don't want to observe potential changes in meaning by adding the prefix for each single case).For cases where the initializer list cannot be used, I came up with
a_
that can be used as a prefix before the original name.