Class members with an underscore (_) prefix

In our projects, we decided on prefix member variables and some private / protected methods with underscores (so with _ ").

During the discussion, it was stated that this is not recommended because of some incompatibilities with some compilers / linkers on some platforms. Since we want to be portable as much as possible, I would like to be sure.

I also believe that the underscore globals prefix in C can be a problem.

The same applies to C ++ - linkage, and if so, in what cases (platforms / compilers / linkers)?

+6
source share
3 answers

From C ++ 03 standard: Β§17.4.3.1.2 / 1

Certain sets of names and function signatures are always reserved for implementation:

  • Each name that contains a double underscore ( __ ) or begins with an underscore followed by an uppercase letter (2.11) is reserved for implementation for any use.

  • Each name starting with an underscore is reserved for implementation for use as a name in the global namespace.

Equivalent text is present in C ++ 11 Β§17.6.4.3.2 / 1

+11
source

Personally, I use m_name , with 'm' for 'member'.

By the way, I use p_ for parameters in my functions and g_ for several inevitable global variables.

(Then I usually get from SO users, because it looks like a Hungarian notation ;-) But this is not so.)

+4
source

Also see here: What are the rules for using underscores in a C ++ identifier?

I saw a lot of code using single underscores as a prefix or double underscore in an indeficator, and it just worked. But you never know. Identifiers are reserved and anything can happen, depending on the compiler.

+2
source

Source: https://habr.com/ru/post/900607/


All Articles