Question

OK I am not a very experienced C++ programmer, but I was wondering what is the significance of the underscores in the arguments of the following constructor?

class floatCoords
 {
 public:
  floatCoords(float _x, float _y, float _width, float _height)
   : x(_x), y(_y), width(_width), height(_height)
  {

  }
  float x, y, width, height;
  ...
Was it helpful?

Solution

It's just a convenient naming convention, it means nothing to the language. Just be sure you don't follow it with an upper-case letter: What does double underscore ( __const) mean in C?

OTHER TIPS

Nothing special. He just named it like that to distinguish between the member variables and parameter names.

Underscore is a valid character in C++ identifiers.

Most likely, the author of the code was trying to avoid the potential conflict between the data member names and constructor parameter names in the initializer list. Quite likely, the author was not aware of the fact that C++ lookup rules make sure that the conflict will not occur anyway. I.e. the following code will also produce expected results

class floatCoords    {
    public:
        floatCoords(float x, float y, float width, float height)
                : x(x), y(y), width(width), height(height)
        {
        }
        float x, y, width, height;
        ...

although it might prove to be confusing for an unprepared reader.

Of course, inside the body of the constructor, parameter names will hide the member names, thus making it necessary to use this->... or qualified names to access the data members. Chances are the author of the code was trying to avoid that as well.

These are just convention of variable naming that different people/institutions assume. This may seem unnecessary at first sight but is actually very useful when reading and writing code, especially when you have parameters and member variables that have the same name. E.g. you can commonly have these three cases:

class A {
  void foo(int age_) { //parameter
    int age = 18; //local scope
    if (age_ > age) cout << legal << endl;
  }
  int _age; //member
};

In the above example:

  • _variable - means this is a class member variable
  • variable_ - means this is a parameter to a function
  • variable - means this is just a regular variable local to the function scope

They are just names for parameters passed in. They happen to match member variables and are used for initializers.

There is no special meaning - it is just a convention some people may use.

They don't have a syntactic meaning. However, if you have a bunch of fields (e.g. x, y, width, and height), it's a common convention to name the constructor parameters that initialize them with the field name plus a leading underscore.

The purpose of those underscores is to distinguish the parameter variable float _x and the member variable float x. Syntactically, there's no special meaning added due to the underscores. I personally prefer to prefix all parameters with a_ and prefix all member variables with m_ when I do have to code C++.

So when I do get into a situation where I have to mix and match local, member, and parameter variables, I know which ones I am dealing with.

int y = a_x * 2;
m_x = y + 3;

because if you would name den float x, float y etc. you could not access the actual fields of the class because they have the same name.

it has nothing to do with the language, it's just a convention by the actual programmer (to make the code more obvious, i think)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top