190k views
2 votes
How to separate a local and private variable of the same name c++?

User Tom Bates
by
6.5k points

1 Answer

1 vote
If you have a private class variable, say private int a; and a local function variable int b; then a and b can be used in that function in the same way, you can't spot the difference. A couple of conventions exist to make this more visible in your program:

- prefix the class member variables with something like m or m_ (just an underscore is done in c# but you can run into clashes with reserved names in c++)
- always dereference them with this->a
User VAr
by
6.1k points