Naming convention for params of ctors and setters

前端 未结 12 1929
轻奢々
轻奢々 2021-01-05 00:42

For those of you who name you member variables with no special notation like m_foo or foo_, how do you name parameters to your ctors and setters?

12条回答
  •  被撕碎了的回忆
    2021-01-05 01:20

    I always do this:

    Obj(int foo) : foo(foo) {}
    

    I used to play games with appending funny symbols until one time I got hit by this:

    Obj(Bar& b) : b_(b_) {}
    

    Can you see the mistake? (Yes, b_ is a private member reference variable). It compiled without a warning. Cost me 3 days of debugging (I was a green programmer then).

    Now I always use the same name to avoid typos (and subsequent crashes) like that. There is no ambiguity inside the initialization list. This part of the language was designed just for this case, so take advantage of it.

提交回复
热议问题