Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

naming convention for public and private variable?

Tags:

c++

Is it wrong to use m_varname as public and the same class with _variable as private

like image 763
yesraaj Avatar asked Feb 26 '09 07:02

yesraaj


2 Answers

Some concerns:

  • Why do you have public variables?

  • Identifiers starting with _ and __ are reserved for system libraries. In practice this doesn't matter very often, but it's nice to be aware.

With those things said, there's nothing wrong with creating a naming convention, regardless of how it looks. Just be consistent.

like image 183
Dan Olson Avatar answered Oct 17 '22 05:10

Dan Olson


The same goes for C++ and for Java: you do not need any hungarian notation nor any prefixes/suffixes. You got keyword "this"!

class MyClass {
    private:
        int value;

    public:
        MyClass(int value) {
            this->value = value;
        }
}

Of course in this simple example you can (should!) use constructor initialization list ;)

So, instead using any awkward notations just employ language's possibilities. When you know the name of your member variable - you know that it is perfect. Why would you obfuscate it with "_"?

As for using the same names for public and private members: this absolutely wrong thinking! Why would one need two things to represent the same in the same class? Make it private, name it perfectly and give getters and setters public.

like image 32
Marcin Gil Avatar answered Oct 17 '22 04:10

Marcin Gil