What is the point of leading underscores in a C ++ constructor?

OK. I'm not a very experienced C ++ programmer, but I was wondering what is the meaning of underscore in the arguments of the next 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;
  ...
+5
source share
8 answers

It’s just a convenient naming convention; it means nothing to the language. Just make sure you don't follow the uppercase letter: What does double underscore (__const) mean in C?

+8
source

Nothing special. He simply called it so as to distinguish between member variables and parameter names.

Underscore is a valid character in C ++ identifiers.

+9

, . , , ++ , .

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

.

, , this->... . , .

+8

, /. , , -, . . :

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

:

  • _variable - , -
  • variable_ - ,
  • variable - , ,
+2

. - .

- , .

+1

. , (, x, y, width height), , , .

0

, float _x - float x. - . a_ - m_, ++.

, , , , , .

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

den float x, float y .., , .

, ( , )

0
source