I was just looking if there is any convention for naming the class constructor parameters if they are similar to member variables (may be with some additional underscore or other such character).
I understand that using the same name is allowed but it looks a bit messy....
I'd be glad to know if there is some convention for the same.
Here is the code using same names for member variables and constructor parameters:
1 2 3 4 5 6 7 8 9
#include <iostream>
class Counter
{
public:
int counterValue;
Counter(int counterValue) : counterValue(counterValue) {} // is there any naming convention/best
// practice for this parameter name
};
One that I know of is prefixing "m_" for member variables.
Ex :
1 2 3 4 5 6 7 8 9
class Counter
{
public:
int m_counterValue;
Counter(int counterValue)
{
m_counterValue = counterValue;
}
};
don't use the same variable. It causes problems if you don't use :: to ensure the correct one is being used in each place. Here, I suspect you have a bug for that very reason.
You probably need this->counterValue = counterValue
Schools and companies provide some guides to your naming conventions, or should. Without those, just use common sense and avoid repeating the name.
well, a lot of folks would say to not even make public class vars, to always make them private and then write a function that effectively makes them public (getters and setters designs)
I don't use the m_ and other Hungarian stuff anymore. Modern IDEs tell you what the item is on a mouse-over, making it just visual clutter.
I was just looking if there is any convention for naming
There is no convention for naming.
Why do you consider using the same name "a bit messy", and, if we agree that is true, why does it imply that it is the name of a member that needs to be modified? Constructor argument names can be modified just as well, and possibly with more freedom.