使用无前缀成员变量时如何命名构造函数参数?

发布于 2024-10-21 14:34:38 字数 523 浏览 6 评论 0原文

当然,没有一种正确的方法可以做到这一点,但我什至想不出任何合适的命名方案,这就是我在这里问的原因。 (所以:虽然所有答案都是主观,但它们仍然有用!)

问题如下:对于简单的聚合结构,我们不使用成员var前缀。

struct Info {
  int x;
  string s;
  size_t z;

  Info()
  : x(-1)
  , s()
  , z(0)
  { }
};

然而,有时提供一个初始化构造函数来初始化结构是有用的,但是,当成员变量本身已经采用了最自然的名称时,我无法为参数想出一个合适的命名方案:

struct Info {
  int x;
  string s;
  size_t z;

  Info(int x?, string s?, size_t z?)
  : x(x?)
  , s(s?)
  , z(z?)
  { }
};

其他人是什么?在这种情况下使用?

Certainly there is no one right way to do this, but I can't even think of any decent naming scheme, that's why I'm asking here. (So: While all answers will be subjective, they will be useful nevertheless!)

The problem is as follows: For simple aggregate structs, we do not use member var prefixes.

struct Info {
  int x;
  string s;
  size_t z;

  Info()
  : x(-1)
  , s()
  , z(0)
  { }
};

It is nevertheless sometimes useful to provide an initializer ctor to initialize the struct, however - I cannot come up with a decent naming scheme for the parameters when the most natural names for them are already taken up by the member variables themselves:

struct Info {
  int x;
  string s;
  size_t z;

  Info(int x?, string s?, size_t z?)
  : x(x?)
  , s(s?)
  , z(z?)
  { }
};

What are other people using in this situation?

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

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

发布评论

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

评论(8

星光不落少年眉 2024-10-28 14:34:39

为什么要发明前置/后缀?我只是使用相同的名字。 C++ 就是为此而设计的。

struct Info {
  int x;
  string s;
  size_t z;

  Info(int x, string s, size_t z)
  : x(x)
  , s(s)
  , z(z)
  { }
};

很简单。

Why invent pre/postfixes? I just use the same name. C++ is designed for that to work.

struct Info {
  int x;
  string s;
  size_t z;

  Info(int x, string s, size_t z)
  : x(x)
  , s(s)
  , z(z)
  { }
};

It's straight forward.

泅人 2024-10-28 14:34:39

使用相同的名称 - 它们不会冲突。

“在构造函数 (12.6.2) 的 mem-initializer 表达式中查找使用的名称时,函数参数名称为
可见并隐藏在包含函数声明的块、类或命名空间范围中声明的实体的名称。” 3.4.1 C++ 2003

Use the same names - they don't collide.

"During the lookup for a name used ... in the expression of a mem-initializer for a constructor (12.6.2), the function parameter names are
visible and hide the names of entities declared in the block, class or namespace scopes containing the function declaration." 3.4.1 C++ 2003

祁梦 2024-10-28 14:34:39

我倾向于使用“a”前缀——如“awhatever”。

Info(int aX, string const & aS, size_t aZ);


struct Time {
  Time(time_t aUnixTime) : UnixTime(aUnixTime) {}
  time_t UnixTime;
};

I tend to use "a" prefix - as in "a whatever".

Info(int aX, string const & aS, size_t aZ);


struct Time {
  Time(time_t aUnixTime) : UnixTime(aUnixTime) {}
  time_t UnixTime;
};
稚然 2024-10-28 14:34:39

我在这里看到的是对构造函数参数使用尾随下划线,例如:

struct Info {
  int i, j;
  Info( int i_, int j_ ) : i( i_ ), j( j_ ) { }
};

Something I've seen around here is using trailing underscores for constructor arguments, e.g.:

struct Info {
  int i, j;
  Info( int i_, int j_ ) : i( i_ ), j( j_ ) { }
};
蓝海似她心 2024-10-28 14:34:39

我正在使用这个:

struct Info {
  int x;
  string s;
  size_t z;

  Info(int x, string s, size_t z)
  : x(x)
  , s(s)
  , z(z)
  { }
};

这可能有点令人惊讶,但这是完全合法的。

另请参阅:

我可以对字段和构造函数使用相同的名称吗参数?

构造函数的参数命名

I am using this:

struct Info {
  int x;
  string s;
  size_t z;

  Info(int x, string s, size_t z)
  : x(x)
  , s(s)
  , z(z)
  { }
};

It may be a bit surprise, but this is perfectly legal.

See also:

Can I use identical names for fields and constructor parameters?

Parameters Naming for Constructor

月棠 2024-10-28 14:34:39

我相信只要您使用初始化列表,就可以使用相同的名称:

struct Info {
  int x;
  string s;
  size_t z;

  Info(int x, string s, size_t z) : x(x) , s(s) , z(z)
  { }
};

如果您必须做一些工作来初始化字段,您仍然可以使用相同的名称,但会少一点方便的:

struct Example {
  char *c;
  size_t l;

  Example(char *c, size_t l) : l(l), c(new char[l])
  {
      // in the block c is the parameter and this->c is the member
      std::copy(c, c + l, this->c);
  }
};

I believe as long as you're using the initialization list, you can use the same names:

struct Info {
  int x;
  string s;
  size_t z;

  Info(int x, string s, size_t z) : x(x) , s(s) , z(z)
  { }
};

If you had to do some work to initialize a field, you could still get away with using the same names, but it would be a little less convenient:

struct Example {
  char *c;
  size_t l;

  Example(char *c, size_t l) : l(l), c(new char[l])
  {
      // in the block c is the parameter and this->c is the member
      std::copy(c, c + l, this->c);
  }
};
我也只是我 2024-10-28 14:34:39

您可以使用与成员名称相同的参数名称(但有些人觉得这很混乱)。

我见过成员使用前缀/后缀(_mmy_ 很流行,在这种情况下与参数没有冲突)或参数(根据我的经验,前缀 p 是最常用的)。

You can use parameter name identical to member names (but some find it confusing).

I've seen the use of prefix/suffix for members (_, m, my_ are popular in which case there is no conflict with parameters) or for parameters (a prefix of p is the most popular for that use in my experience).

鹿童谣 2024-10-28 14:34:39

如果只是将成员设置为相应参数的值,请使用初始值设定项列表。这里成员变量和参数使用相同的名称是绝对安全的。如果不能简单地赋值,而必须调用函数,那么让成员和参数的名称易于区分是至关重要的,同时也易于使关系可见,这里前缀或后缀是一个不错的选择。

Erik的回答建议使用前缀a,但我发现更改第一个字母需要额外的工作或将原始名称改为大写以使参数名称非常烦人,它会阻止简单地使用复制和粘贴(让它保持不变不是一个选项,因为您不想通过为每个单独的情况添加前缀来观察含义的潜在变化)。

对于无法使用初始化列表的情况,我想出了 a_ ,它可以用作原始名称之前的前缀。

struct Info {
    int value;
    char description[MAX_DESCRIPTION_SIZE];

    Info(int value, char* a_description)
    : value(value)
    {
        someSafeCopy(description, a_description);
    }
};

In cases where members are simply set to the values of the corresponding parameters, use the initializer list. Here using the same name for member variable and parameter is absolutely safe. If you cannot simply assign values, but have to call functions, it's vital to make the names of members and parameters easy to distinguish, but also easy to make the relation visible, here a prefix or postfix is a good option.

Erik's answer suggests the prefix a, but I find the extra-work to change the first letter or the original name to upper case for getting the parameter name extremely annoying, it prevents simply using copy&paste (Letting it unchanged is not an option because you don't want to observe potential changes in meaning by adding the prefix for each single case).

For cases where the initializer list cannot be used, I came up with a_ that can be used as a prefix before the original name.

struct Info {
    int value;
    char description[MAX_DESCRIPTION_SIZE];

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