开发者

How to name constructor parameters when using non-prefixed member variables?

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?


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


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.


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


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_ ) { }
};


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


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);
  }
};


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


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);
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜