Should I use the same name for a member variable and a function parameter in C++?

前端 未结 9 1827
自闭症患者
自闭症患者 2020-12-08 03:08

I am wondering if it is a good practice to use the same name for both a member variable and a function parameter in C++.

I come from a Java backgro

9条回答
  •  情书的邮戳
    2020-12-08 03:57

    I find it a good choice to give member variables the same name as constructor initialization parameters.

    Here are my reasons:

    • reduces the number of identifiers, hence reduces the complexity
    • you don't need to invent so many identifiers
    • same things should have same name if possible, that is logically speaking, I know
      parameter != member
    • contexts and indices can allow to give the same name to the same thing
    • you more easily find references (identifiers) to the logical thing by searching, if all references have the same name

提交回复
热议问题