Reputation: 1721
The large majority of my programming knowledge is self-taught, so I was never taught proper design patterns, conventions, and so on and so forth.
I've been digging through a lot of my company's software libraries lately, and I notice that a lot of class data members have underscores in their names.
For example:
class Image
{
// various things
// data members
char* _data;
ImageSettings* _imageSettings;
// (and so on...)
};
I see this in a lot of online example code as well. Is there a reason behind this convention? Sorry I couldn't provide better examples, I'm really trying to remember off the top of my head, but I see it a lot.
I am aware of Hungarian notation, but I am trying to get a handle on all of the other conventions used for C++ OOP programming.
Upvotes: 6
Views: 11121
Reputation: 14505
I don't think there is a universal rule for naming. However, one of the most important one is to stick to what's already used in your company/project. Don't break it. Otherwise, it's most likely that your tech lead or mentor will challenge you about it.
For your reference, Google C++ style for naming
Upvotes: 1
Reputation: 206518
Usually an underscore is used in member variables so as to distinguish between member variables, static member variables & local variables.
m_
is used for normal member variables &
s_
for static member variables
This way the scope of the variable is visible in the variable name itself.
Also, sometimes underscores are used in member name so that you can name your get
and set
methods with the member name itself.
For example:
class Image
{
// various things
// data members
char* _data;
ImageSettings* _imageSettings;
// (and so on...)
public:
ImageSettings* imageSettings()
{
//return pointer
}
void imageSettings(ImageSettings *ptr)
{
//set member variable value
}
};
However, different organizations adopt different conventions & coding styles and one should stick to them. Follow the principle,
When in Rome think & act like the Romans :)
Upvotes: 2
Reputation: 69988
I have seen _
typing in front of the member, just to notify the reader that it's a class member variable.
More conventional way I have seen is putting m_
; i.e. m_data
;
Upvotes: 4
Reputation: 10969
It is simply intended to make it clear what variables are members, and which are not. There is no deeper meaning. There is less possibility for confusion when implementing member functions, and you are able to chose more succinct names.
void Class::SomeFunction() {
int imageID;
//...
SetID(imageID + baseID); //wait, where did baseID come from?
}
Personally, I put the underscore at the end instead of the begining [if you accidently follow a leading underscore with a capital letter, your code becomes ill formed]. Some people put mBlah
or m_Blah
. Others do nothing at all, and explicitly use this->blah
to indicate their member variables when confusion is possible. There is no global standard; just do what you want for private projects, and adhere to the existing practices elsewhere.
Upvotes: 6
Reputation: 9179
Usually underscore before a member is used when the member is private.
It is usefull in language that does not have a builtin way to declare members private, like python.
Upvotes: 3
Reputation: 14031
The reason for this convention is that member names beginning with underscores show up first in Intellisense. Generally, you have to follow the convention of the project you are contributting to. If you are starting a new project, it is a good idea to follow a commonly accepted convention, such as Sutter's and Alexandrescu's C++ Coding Standards.
Upvotes: 0
Reputation: 2112
What I've learned is that having an underscore before or after a variable means that it's a member of that class - private, public, or protected.
Upvotes: 0