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

前端 未结 9 1794
自闭症患者
自闭症患者 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:38

    Do it this way:

    class Player
    {
        public:
        void setState(PlayerState *state)
        {
            this->m_state = state;
        }
    
        private:
           PlayerState *m_state;
    }
    

    You'll thank me some time later on. Haha.. (:

    The "m_" ("Member") prefix distinguish members from functions and other stuff. Very useful with stuff like intellisense (or any other IDE auto-suggestion).

    Also, mark m_state as const if you do not intend to change it later. Just in case.

提交回复
热议问题