I have to add a default int& argument (Eg iNewArgument) to an existing function definition in a header file:
What is the syntax to initialize the last argument to make it take a default value?
..
virtual int ExecNew
(int& param1, int& RowIn,int& RowOut, char* Msg = '0',
bool bDebug=true, int& iNewArgument = NULL ) = 0 ;
.
.
NULL is #defined to 0
Error: default argument: cannot convert from
inttoint&
Your example indicates that you are creating a virtual function. Default arguments in virtual (especially pure) functions are not a good idea, because they are not considered when you call an overridden function.
Now, if someone calls
fon aBobject, he will have to provide the argument – the default isn’t used. You would have to duplicate the default argument in the derived class, which creates ugly redundancies. Instead, you can use the non-virtual interface patternThe user can now call
fwith one, and with two arguments, from whatever class type he calls the function, and the implementers ofAdon’t have to worry about the default argument. By using overloading instead of default arguments, you don’t need to break your head around lvalues or temporaries.