Should I use the same name for a member variable and a function parameter in C++?
That is correct, and allowed by the Standard. But a better approach is to use some naming-convention for member variables. For example, you could use m_
prefix for all member variables, then anyone could infer what m_state
is. It increases the readability of the code, and avoids common mistakes.
Also, if m_state
is the member, then you don't have to write this->m_state = state
in the member function, you could just write m_state = state
. In your current code, this->
part becomes necessary, without which state = state
will become self-assignment.
Normally people just put an underscore after the variable or use shorter less descriptive var names for the function parameter.
I personally do not like the same name thing because when reading it, it is easy to make mistakes.