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?
Parameter NamesThe name of a parameter must be unique in its scope. It cannot be the same as the name of another parameter for the same method or constructor, and it cannot be the name of a local variable within the method or constructor. A parameter can have the same name as one of the class's fields.
in short.. it's fine to not initialize your member variables in the constructor as long as you initialize them somewhere in the class before using them.. Save this answer.
Const member variables must be initialized. A member initialization list can also be used to initialize members that are classes. When variable b is constructed, the B(int) constructor is called with value 5. Before the body of the constructor executes, m_a is initialized, calling the A(int) constructor with value 4.
The value of a Variable can be accessed using the symbol prefix ##. Both Local and Global Variables can be retrieved using ##.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With