Search code examples
c++initializationnaming-conventionsinitializer-list

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?


Solution

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