How to define constructor parameters when using variables with a member prefix?

Of course, there is no right way to do this, but I can't even think of any decent naming scheme, which is why I ask here. (So: while all answers are subjective , they will be useful , nonetheless!)

The problem is this: for simple aggregate structures, we do not use the var member prefix.

struct Info { int x; string s; size_t z; Info() : x(-1) , s() , z(0) { } }; 
Nevertheless, it is sometimes useful to provide a ctor initializer to initialize the structure, however - I cannot come up with a suitable naming scheme for parameters when the most natural names for them are already taken 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 do other people use in this situation?

+6
c ++ initialization naming-conventions initializer-list
source share
9 answers

Why reinvent pre / postfixes? I just use the same name. C ++ is designed 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 is straight forward.

+7
source share

Use the same names - they do not collide.

"When searching for the name used ... in the mem-initializer expression for the constructor (12.6.2), function parameter names are the visible and hidden names of objects declared in blocks, classes, or namespace spaces containing the function declaration." 3.4.1 C ++ 2003

+12
source share

I usually use the prefix "a" - as in "any."

 Info(int aX, string const & aS, size_t aZ); struct Time { Time(time_t aUnixTime) : UnixTime(aUnixTime) {} time_t UnixTime; }; 
+4
source share

I believe that while you use 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 needed to do some work on initializing the field, you could still avoid using the same names, but that 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); } }; 
+3
source share

I use this:

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

This may be a bit of a surprise, but it is completely legal.

See also:

Can I use identical names for fields and constructor parameters?

Constructor Naming Options

+3
source share

Something I saw here is using trailing underscores for constructor arguments, for example:

 struct Info { int i, j; Info( int i_, int j_ ) : i( i_ ), j( j_ ) { } }; 
+2
source share

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

I saw that using a prefix / suffix for members ( _ , m , my_ is popular, in this case there is no conflict with parameters) or for parameters (the p prefix is ​​the most popular for this use in my experience).

+1
source share

I have seen var_ for use in several base codes. In your case, this will give x_, s_ and z _.

0
source share

In cases where members are simply set to the appropriate parameters, use a list of initializers. Here, the same name for the member variable and parameter is absolutely safe. If you cannot just assign values, but they have to call functions, it is important that the names of the participants and parameters are easily distinguishable, but it is also easy to make the relation visible, here the prefix or postfix is ​​preferable.

Eric's answer suggests the prefix a , but I find the extra work of changing the first letter or the initial name in uppercase to get the parameter name is very annoying, it prevents the simple use of copy & paste (if you leave it unchanged, this is not an option because you don’t want to observe potential changes in value by adding a prefix for each individual case).

In cases where the initializer list cannot be used, I came up with a_ , which can be used as a prefix in front of 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
source share

All Articles