What naming convention to follow for constructor parameters with same name as class member variables.

Hi people :),

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;
        }
};


Are there any other conventions.. ?



Thanks :)
Last edited on
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.
Last edited on
Thanks jonnin for your response.

I have edited the question and using initialization list which eliminates the problem of name conflicts/overshadowing and the need for using "this->".

I am just for if there is any standard naming conventions for member variables and constructor variables.

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;
        }
};


Are there any other conventions.. ?

thanks :)
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.


kapil2905 wrote:
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.
Last edited on
Thanks @Cubbi for the well formed response :)
Topic archived. No new replies allowed.