C++ 中前导下划线的含义是什么?构造函数?

发布于 2024-08-08 10:56:20 字数 268 浏览 8 评论 0原文

好吧,我不是一个非常有经验的 C++ 程序员,但我想知道以下构造函数的参数中下划线的意义是什么?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

﹎☆浅夏丿初晴 2024-08-15 10:56:20

没什么特别的。他这样命名只是为了区分成员变量和参数名称。

下划线是 C++ 标识符中的有效字符。

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.

肥爪爪 2024-08-15 10:56:20

这只是一个方便的命名约定,对语言没有任何意义。请确保后面没有大写字母: 双下划线(__const)在 C 中意味着什么?

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?

烟燃烟灭 2024-08-15 10:56:20

最有可能的是,代码的作者试图避免初始化列表中的数据成员名称和构造函数参数名称之间的潜在冲突。作者很可能没有意识到 C++ 查找规则可以确保无论如何都不会发生冲突。即,下面的代码也将产生预期的结果,

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->... 或限定名称来访问数据成员。代码的作者很可能也试图避免这种情况。

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.

雨落星ぅ辰 2024-08-15 10:56:20

这些只是不同的人/机构假设的变量命名约定。乍一看这似乎没有必要,但实际上在阅读和编写代码时非常有用,特别是当您具有同名的参数和成员变量时。例如,您通常可以遇到以下三种情况:

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

在上面的示例中:

  • _variable - 表示这是一个类成员变量
  • variable_ - 表示这是函数变量的参数
  • - 表示这只是函数范围内的常规变量

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
薄荷→糖丶微凉 2024-08-15 10:56:20

它们只是传入参数的名称。它们恰好与成员变量匹配并用于初始值设定项。

没有什么特殊含义——这只是一些人可能使用的约定。

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.

若相惜即相离 2024-08-15 10:56:20

它们没有语法意义。但是,如果您有一堆字段(例如 xywidthheight),那么命名构造函数参数的通用约定,这些参数使用字段名称加前导下划线来初始化它们。

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.

枕梦 2024-08-15 10:56:20

这些下划线的作用是区分参数变量float_x和成员变量float x。从语法上讲,下划线并没有添加任何特殊含义。当我必须编写 C++ 代码时,我个人更喜欢为所有参数添加 a_ 前缀,并为所有成员变量添加 m_ 前缀。

因此,当我确实遇到必须混合和匹配本地变量、成员变量和参数变量的情况时,我知道我正在处理哪些变量。

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

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;
顾挽 2024-08-15 10:56:20

因为如果您将名称命名为 den float x、float y 等,您将无法访问该类的实际字段,因为它们具有相同的名称。

它与语言无关,这只是实际程序员的约定(我认为是为了使代码更明显)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文