Variables without a name in the constructor parameter?

I would like to know when someone gives you a template for the default constructor in C ++, and there are a number of unnamed variables in the parameter that are somehow assigned to the values. What does it mean? Is it legal? How can I access these variables?

Example I got:

IntSet::IntSet(int = -1, int = -1, int = -1, int = -1); //default constructor 

I would be grateful for any answers explaining this use!

+4
source share
2 answers

This means that if you call it without parameters, the parameters will take the values ​​that you passed.

So if you call

 IntSet* i = new Intset(); 

will be equivalent to calling

 Intset* i = new IntSet(-1,-1,-1,-1) 

About the nameless part. I would suggest, because it is part of a library that uses templates. Unconditional parameters are not used, but exist, so they correspond to the signature of another class that they may need. In case they need it, it will by default simply skip -1. You can take a look at this link for an example:

http://compgroups.net/comp.lang.c++/unnamed-formal-parameter/1004784

I am copying an example from the link above to make the case of using such a construct a useful way

for instance

 class A { public: A(void* = 0) {} // unused parameter }; class B { public: B(void* p) : pp(p) {} // used parameter private: void* pp; }; template <class T> class C { public: static T* create(void* p = 0) { return new T(p); } }; int main() { A* a = C<A>::create(); B* b = C<B>::create("hello"); } 

C :: create will not compile if A :: A does not have a parameter, even if it is not used.

+1
source

It is perfectly legitimate not to indicate arguments in a declaration or in a function definition. It is also legitimate to use different names for this argument in the declaration and definition.

Note that if a parameter not named in the definition is not available. Why do this? Many compilers may complain about unused arguments. Most compilers will not complain if the function definition does not name these unused arguments.

About unnamed parameters in the declaration, which is the topic of this question: just because it is legal does not necessarily mean that it is good practice. These unnamed arguments in a function declaration may be good for the compiler, but they are not suitable for human reading. No name = zero communication. The documentation and the header file should contain everything that the external user should know about using the class. RTFM and possibly RTFH (read the exact manual / read the subtle headlines). This word β€œF” (β€œfine”) in RTFM and RTFH has a different meaning in RTFC.

These unnamed heading arguments are almost inevitably linked to poor documentation. As an external user, this makes me read the definition of a function in order to understand how to use the function. RTFC It is not normal for an external user to read an implementation to determine what is going on.

+5
source

Source: https://habr.com/ru/post/1439588/


All Articles