Initializing parameter of a class method with value during declaration C++ - c++

I am working on another persons C++ code and don't understand why they did something.
They have a notification class that has a public method called check which takes in the time as a parameter
void check(unsigned long time = 1);
Every time the method is called in the code a current time is passed to it.
notification1.check(currentTime);
My question is why did they give the parameter a value when they defined the method? Why not just say,
void check(unsigned long time);

That's because if no argument if provided in function call, default argument value will be used for time which is 1 in this case. This is called Default Argument
If you call check function and provide no argument, default value of 1 will be assigned to time by the compiler. If you pass an argument while calling check function, that passed value will override the default value of time.
When a function has a default argument passed in to it, you are not required to pass an argument while calling that function and you can specify as many default arguments as you want.

Do you have access to the implementation of check function?
since there is default argument specified in function declaration, you can call it without arguments as well. (ex: notification1.check() ) you should explore what is the behaviour of the check(...) function in each of following scenarios
Function call WITH an argument? (i.e. notification1.check(currentTime))?
Function call WITHOUT argument? (i.e. notification1.check())?
As a best practice of coding, these behaviours has to be documented with the member function declaration. if it is not the case, you'll have to explore the implementation.

It's a default argument. http://en.cppreference.com/w/cpp/language/default_arguments explains all you ever wanted to know about this C++ feature.
My question is why did they give the parameter a value when they defined the method?
If the default argument is not used anywhere, i.e. if the function is always called with a non-default argument, then chances are it was a wrong design decision. Perhaps they thought the default argument was useful when they originally designed the code, but it now turned out that there is no useful default value. Or perhaps they think that the default argument may still be useful in the future.
As a matter of fact, 1 is a questionable value for an unsigned long time parameter, which looks a lot like a value representing milliseconds since January 1st 1970. Why not 0?

Related

passing variable without a name to function

I saw a function written in the following manner:
retVal PreProcessor::TxSlotCB(void *a_pClass, PRE_PARAMS &/*commonParam*/)
{
some code
}
struct PRE_PARAMS
{
uint32_t param1;
uint32_t param2;
};
what is happening in the second parameter? how can it be empty? and is there any way to access to it?
In C++, a formal parameter can be given but anonymous. This means that the actual argument should be given but is not used in the called function.
So it should be given in calling context, it is transmitted, but the called function cannot and does not use it. And the compiler won't give any warnings.
You cannot access it in the body of the function. If you need to access it, change the declaration of the formal parameter to give it some name.
This means that parameter of type PRE_PARAM is not used by this function currently.
So, what happens is when you design a function you decide on the parameters this function would take to fulfill it's purpose.
But later you find that this parameter is not of much significance to this function. However, removing this parameter from function declaration is a tedious job as you have to check for all calls to this function and make sure they confirm to that change.
So, a better way is to not provide the name for argument in function's definition making that parameter redundant.

variadic function - how to ensure parameters passed correctly

Is there any way (built-in or a code pattern) to ensure that a variadic function is passed the correct number of parameters? (This will be included as part of an API obviously, I can check my own internal code.)
I was considering requiring a UN32 Magic Number to be the last argument passed and check that for validity in the variadic function. Does anyone have any thoughts on that?
va_* macros just pop the variables from the local stack, so you have to trust the user. passing an array/size tuple could be safer, I think.
You could use the PP_NARG macro to add a count semi-automatically.
int myfunc (int count, ...);
#define MYFUNC(...) myfunc(PP_NARG(__VA_ARGS__), __VA_ARGS__)
MYFUNC(a,b,c,d);
MYFUNC(a,b,c,d,e,f,g);
gcc -E produces:
int myfunc (int count, ...);
myfunc(4, a,b,c,d);
myfunc(7, a,b,c,d,e,f,g);
There is no definitive way in C or C++ to ensure that the correct number of arguments have been passed to a variadic function. Even requiring a signature is not guaranteed to work as it may clash with the value of a valid argument. You will probably be much better off passing a vector<> as the element count retrieved is accurate.
Couldn't you use the variadic template feature of C++0x applied to a function? This would generate a vararg function that is type-safe .
See this link with it's type-safe printf implementation using a variadic templated function
http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates
It depends on what you mean by "ensure that a variadic function is passed the correct number of parameters"...
Passing a UN32 Magic Number as last argument will allow you to determine where the list of arguments ends, so their overall number. So, by counting how many arguments you have found before UN32, you know how many arguments you have and your function should know whether is it enough. Don't know if it is ok for you to determine this at run-time (it could be too late)...
Anyway, usually variadic functions have a fixed argument list portion representing the mandatory arguments (at least one); so possibly this should be the way for you to ensure that the function gets the correct number of arguments...
No. Not possible.
Variadic breaks type-safety in a way that cannot be fixed.
If you want type-safety back, then consider breaking variadic function into several typesafe
member function of a [small] class that holds the shared state between their calls.
This is always possible, even if multiple calls might look awkward compared to single variadic
call.
Shared state is probably why you wanted variadic function in the first place.
Take iostream vs printf as example.

about argument to member function

Will adding an argument to member function gonna change the behavior of the function?
Need help about this concept.
The behavior won't change if you only add an argument to the prototype of the function.
However you will have to change all the call to this function since the prototype changed, this isn't the same function anymore.
And sincerely, I don't see the point of adding an argument if you don't modify the code afterward.
You'll need to give us more information. Like an example. But say you add a new argument (and provide a default), and now argument will be "constructed" and destructed everywhere the member function was called. So it's possible that that in itself has side-effects (e.g. print to console on creationg and destruction).

Default arguments and variadic functions

Is there any way to specify a default parameter in a variadic function?(Applies to templates also)
In C++ you can replace the variadic function with one based on the Named Parameter Idiom.
See the C++ FAQ item 10.20 What is the "Named Parameter Idiom"?.
That gives you default functionality & convenient notation.
Cheers & hth.,
Why would you need both variadic and default params?
For example,
myFunc(int a=5, int b=5, int c=5);
can receive 0-3 parameters with default values, and
myFunc(...)
can reveive any number of parameters. Inside the function, you can check for missing parameters and fill in the default values as required.
First a C++ answer.
A default parameter is a parameter for which you will know that the function should and will see as provided. So you should definitively name these parameters and then may provide default arguments. This would be a "short" version of your function.
If in addition to these default arguments behind you want to have the possibility of having a va_arg argument list just overload your function with a second version that does exactly this. For that "long" version you have to provide all arguments anyhow, so there would be no sense in having default arguments, here.
Now a C answer
Probably you were not looking into such a thing, but with the va_arg macro features of C99 it is possible to define default arguments for functions in C, too. The macro syntax then is more permissive than it is for C++ in that you may also omit arguments in the middle of a function call, not only at the end. So if you would have declared your function something like
int toto(int a, ...)
and specified default arguments for positions 2 and 3, say, you could call it as
toto(4,5,,,37);
So in this sense in C it is possible to do what you asked for. I personally would certainly hesitate to do this.
No there is not way of doing that.

Function argument already initialized in function declaration C++

So here's my question in the function declaration there is an argument and it is already initialized to a certain value. What are the procedures to call this function and use that default value, or is it just a way to document code, telling other programmers what you expect them to use as a value for the parameter? Thank you.
enum File
{
XML = 0,
PDF = 1,
};
Char *SetSection(const File = XML);
If I understand your question correctly, simply calling SetSection with no parameters will do.
SetSection();
The above call gets translated (for lack of a better term) to:
SetSection(XML);
It means that the function can be called without parameters in which case the default value, XML, will be used.
In this case, File is a default argument. Calling SetSection() without arguments will set the File argument to the default value specified in the declaration.
If you call
SetSection();
It will call SetSection(XML) instead.
This is why the optional parameters have to be at the end of all parameters. If you don't provide enough parameters, it will use the default.
XML is the standard parameter.
You can call it with SetSection(); (But SetSection(XML) or SetSection(PDF) are valid, too).
What you are seeing in the declaration is a default argument value.
If you call SetSection(); it is the same as calling SetSection(XML);
You can also call SetSelection(PDF); or use any other valid parameter to override the default.
You may also be seeing the result of an incremental development which started with the function having no parameter and calls to the function scattered throughout the code. Then the alternative file type of PDF was introduced, but the prototype was changed to have a default value, which meant not having to change the existing call site.