简体   繁体   English

在类中使用相同的变量可以在构造函数C ++中使用吗?

[英]Is using the same variable in a class ok to use in the constructor C++?

If I have a class: 如果我上课:

class className{
    int i;
public:
    className(int value);
};

What is considered as the best practise for initializing the class variable 'i' from the constructor as per the below choices? 按照以下选择,从构造函数初始化类变量“ i”的最佳实践是什么?

1) Use the actual field name with an underscore: 1)使用带下划线的实际字段名称:

className::className(int i_){
    i = i_;
}

2) Use the actual field name with "this": 2)将实际的字段名称与“ this”一起使用:

className::className(int i){
    this->i = i;
}

3) Completely inconsistent things like: 3)完全不一致的事情,例如:

className::className(int value){
    i = value;
}

I have seen this question being directly addressed for Java but not so much for C++. 我已经看到这个问题直接针对Java解决,但对于C ++则没有那么多。 I ask because I would favor number 2 as I would personally prefer less variable names being made. 我问是因为我更喜欢数字2,因为我个人更喜欢使用较少的变量名。 However I would like to know what further considerations this could mean for the compiler or linker etc. I would also like to stick with the C++ norm. 但是,我想知道这对编译器或链接器等可能意味着什么。我还想坚持使用C ++规范。

Many Thanks! 非常感谢!

Yes that's ok. 是的,没关系。

Some people actually think it idiomatic. 有些人实际上认为它是惯用的。

However, your samples all lack the use of initializer lists :) 但是,您的示例都缺少初始化列表的使用方法:)

class className{
    int i;
public:
    className(int value) : i(value) {};
};

I suggest to avoid the confusion with duplicate names. 我建议避免名称重复。 It makes the compiler complain if you accidentally mess up. 如果您不小心弄乱了代码,它会使编译器抱怨。

The best practice is to initialize your member variables in the initializer list: 最佳实践是在初始化列表中初始化成员变量:

className::className(int i_) : i(i_){}
                               ^^^^^

Reasons : 原因

  1. Performance : You avoid unnecessary calls to members' default constructors. 性能 :避免不必要地调用成员的默认构造函数。
  2. Having Members not default constructible: If you have member variables that aren't default constructible (ie, they don't have a default constructor), you are obliged to initialize them in the initializer list. 使成员不能默认构造:如果成员变量不是默认构造的(即,它们没有默认构造函数),则必须在初始化列表中对其进行初始化。
  3. Having Members const -qualified: Same as 2. 具有成员const合格的成员:与2相同。
  4. Having Members references to objects: Same as 2. 让成员引用对象:与2相同。
  5. Readability : Opinion based. 可读性 :基于意见。
  6. Extensibility : Opinion based. 可扩展性 :基于意见。

As far as it concerns the naming issue: IMHO, it's primarily opinion based. 至于命名问题:恕我直言,它主要基于意见。 Personally, for parameters of a constructor I use the suffix underscore as well. 就个人而言,对于构造函数的参数,我也使用后缀下划线。

I am in agreement with @sehe, to clarify on his context of initializer lists: 我同意@sehe,以澄清他的初始化列表的上下文:

className::className(int i_) : i(i_) {}

However! 然而! I think the identifier names are backwards in terms of appropriateness. 我认为标识符名称在适当性方面是倒退的。 i_ should be the private member variable, and i should be the constructor parameter. i_应该是私有成员变量,而i应该是构造函数参数。

My notes on each "choice": 1) It's easy to see which parameters correspond with one another here 2) It's explicitness here 3) I think you've already concluded your opinion on this one by wording it 'Completely Inconsistent' :). 我对每个“选择”的注释:1)在这里很容易看出哪些参数彼此对应2)在这里很明确3)我认为您已经通过措辞“完全不一致”来总结您的观点:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM