问题
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 background, where this was common. I am wondering if in C++ there are drawbacks doing the following (the code works):
class Player
{
public:
void setState(PlayerState *state)
{
this->state = state;
}
private:
PlayerState *state;
}
Thank you for the answers. As I understand while it works, a better practice would be to put some kind of marker to differentiate member variable from function parameters like:
_ or m_
In some editors (like Qt Designer), member variables are shows in a different color. This is why it did not seem necessary to add any prefixes.
回答1:
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.
回答2:
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.
回答3:
There is not really any difference between the C++ and java, the only drawback is that you have to type this->state = state instead of state = arg. But your code is perfectly acceptable, it's more of styling than anything else.
回答4:
I find it a good choice to give member variables the same name as constructor initialization parameters. Here are my reasons:
- reduces the number of identifiers = reduces the complexity
- you don't need to invent so many identifiers
- same things should have same name if possible, that is
logically speaking, I know parameter != member.
- contexts and indices can allow to give the same name to the same thing
- you more easily find references (identifiers) to the logical thing by searching, if all references have the same name
回答5:
I would suggest you to follow some coding style convention. Personally I use the:
class Player
{
public:
void setState(PlayerState *state)
{
_state = state;
}
private:
PlayerState* _state;
}
回答6:
This is more a style issue than anything else. Most of the time,
there's no issue: state is a very poor name for a variable or a value,
as variables and values should be qualified nouns, e.g.:
void setState( PlayerState* newState )
{
currentState = newState;
}
In theory, anyway. In practice, I've found it useful to use prefixes, along the lines of:
class Player
{
PlayerState* myState;
public:
void setState( PlayerState* newState )
{
myState = newState;
}
};
When reading the code, if the name starts with my, it's clearly a
member variable (and if it starts with our, it's a static member
variable).
Note too that in the constructor, you can do things like:
Player::Player( PlayerState* state )
: state( state )
{
}
I'm not sure what this does for readability, however:
Player::Player( PlayerState* initialState )
: myState( initialState )
{
}
looks a lot clearer (but for simple data holders, the distinction might not be so significant).
回答7:
Please note that some compilers (vs 2015) may generate a warning if a variable shadows another. Off course, one can disable these kind of warnings. But I think it is good practice to have these checks enabled.
回答8:
It's fine, in-fact it might even be good form, so long as it's only ever in your constructor.
回答9:
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.
来源:https://stackoverflow.com/questions/10250016/should-i-use-the-same-name-for-a-member-variable-and-a-function-parameter-in-c