Programing

`m_` 변수 접두사는 무엇을 의미합니까?

crosscheck 2020. 7. 5. 08:03
반응형

`m_` 변수 접두사는 무엇을 의미합니까?


나는 종종 볼 m_접두사 변수에 사용 ( m_World, m_Sprites주로 게임 개발에 관련된 튜토리얼, 예 및 기타 코드, ...).

사람들 m_이 변수 에 접두사 추가하는 이유는 무엇 입니까?


멤버 변수 인 변수를 정의하기위한 일반적인 프로그래밍 방법입니다. 따라서 나중에 사용하는 경우 범위를 알기 위해 정의 된 위치를 볼 필요가 없습니다. 범위를 이미 알고 있고 intelliSense 와 같은 것을 사용하고 있다면 시작할 수 m_있으며 모든 멤버 변수 목록이 표시됩니다. 헝가리 표기법의 일부는 여기 예제의 범위에 대한 부분을 참조 하십시오 .


에서 클린 코드 : 애자일 소프트웨어 장인의 수첩 이 접두사의 사용에 대한 명시 적 권고가있다 :

또한 멤버 변수에 m_더 이상 접두사를 지정할 필요가 없습니다. 클래스와 함수는 필요하지 않을 정도로 작아야합니다.

이것의 예 (C # 코드)도 있습니다 :

나쁜 습관 :

public class Part
{
    private String m_dsc; // The textual description

    void SetName(string name)
    {
        m_dsc = name;
    }
}

좋은 연습:

public class Part
{
    private String description;

    void SetDescription(string description)
    {
        this.description = description;
    }
}

우리는 명시 적 모호성 (의 경우 멤버 변수를 참조하는 언어 구조와 계산 , description멤버 및 description매개 변수) this.


C ++에서는 일반적인 관행입니다. C ++에서는 멤버 함수와 멤버 변수에 동일한 이름을 사용할 수없고 getter 함수는 종종 "get"접두사없이 이름이 지정되기 때문입니다.

class Person
{
   public:
      std::string name() const;

   private:
      std::string name; // This would lead to a compilation error.
      std::string m_name; // OK.
};

main.cpp:9:19: error: duplicate member 'name'
      std::string name;
                  ^
main.cpp:6:19: note: previous declaration is here
      std::string name() const;
                  ^
1 error generated.

http://coliru.stacked-crooked.com/a/f38e7dbb047687ad

"m_" states for the "member". Prefix "_" is also common.

You shouldn't use it in programming languages that solve this problem by using different conventions/grammar.


The m_ prefix is often used for member variables - I think its main advantage is that it helps create a clear distinction between a public property and the private member variable backing it:

int m_something

public int Something => this.m_something; 

It can help to have a consistent naming convention for backing variables, and the m_ prefix is one way of doing that - one that works in case-insensitive languages.

How useful this is depends on the languages and the tools that you're using. Modern IDEs with strong refactor tools and intellisense have less need for conventions like this, and it's certainly not the only way of doing this, but it's worth being aware of the practice in any case.


As stated in the other answers, m_ prefix is used to indicate that a variable is a class member. This is different from Hungarian notation because it doesn't indicate the type of the variable but its context.

I use m_ in C++ but not in some other languages where 'this' or 'self' is compulsory. I don't like to see 'this->' used with C++ because it clutters the code.

Another answer says m_dsc is "bad practice" and 'description;' is "good practice" but this is a red herring because the problem there is the abbreviation.

Another answer says typing this pops up IntelliSense but any good IDE will have a hotkey to pop up IntelliSense for the current class members.


As stated in many other responses, m_ is a prefix that denotes member variables. It is/was commonly used in the C++ world and propagated to other languages too, including Java.

In a modern IDE it is completely redundant as the syntax highlighting makes it evident which variables are local and which ones are members. However, by the time syntax highlighting appeared in the late 90s, the convention had been around for many years and was firmly set (at least in the C++ world).

I do not know which tutorials you are referring to, but I will guess that they are using the convention due to one of two factors:

  • They are C++ tutorials, written by people used to the m_ convention, and/or...
  • They write code in plain (monospaced) text, without syntax highlighting, so the m_ convention is useful to make the examples clearer.

To complete the current answers and as the question is not language specific, some C-project use the prefix m_ to define global variables that are specific to a file - and g_ for global variables that have a scoped larger than the file they are defined.
In this case global variables defined with prefix m_ should be defined as static.

See EDK2 (a UEFI Open-Source implementation) coding convention for an example of project using this convention.


One argument that I haven't seen yet is that a prefix such as m_ can be used to prevent name clashing with #define'd macro's.

Regex search for #define [a-z][A-Za-z0-9_]*[^(] in /usr/include/term.h from curses/ncurses.

참고URL : https://stackoverflow.com/questions/13018189/what-does-m-variable-prefix-mean

반응형