Warning about hiding member variables?

后端 未结 3 1131
一向
一向 2020-12-11 04:17

The following code snippet has a memory leak that I spent too much time chasing down. The problem is that inside Foo(), the local variable x_ hides the member variable x_.

相关标签:
3条回答
  • 2020-12-11 04:41

    Use -Wshadow.

    By the way, neither -W nor -Wall enables -Wshadow.

    It's nice to have the compiler help avoid this kind of problem, but that won't even be necessary if you use conventions that help avoid creating it in the first place, such reserving names of the form x_ for member variables, not local variables.

    0 讨论(0)
  • 2020-12-11 04:42

    We use these warts on the beginnings of names - a_ argument d_ data member s_ static data in class f_ static data in file

    ... and no wart for local variables.

    Truly, the Lakos book is your friend.

    0 讨论(0)
  • 2020-12-11 04:50

    FWIW I wouldn't have this problem because I use a naming convention to distinguish member data from local variables: my member data identifiers are invariably prefixed with m_.

    0 讨论(0)
提交回复
热议问题