huh?????
i dont understand wat u want...can u explain a bit more..exactly wat do you want???
R. Thomas
"Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
"Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18
I want to call a function but this function should not force me insert arguments. if I will decide to insert parameters, fine .
if i will decide not insert parameters than the function will use my default parameters and values.
for example look at the function of microsoft (CDialog):
/* VA.C: The program below illustrates passing a variable
* number of arguments using the following macros:
* va_start va_arg va_end
* va_list va_dcl (UNIX only)
*/
#include <stdio.h>
#include <stdarg.h>
int average( int first, ... );
void main( void )
{
/* Call with 3 integers (-1 is used as terminator). */
printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );
/* Call with 4 integers. */
printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );
/* Call with just -1 terminator. */
printf( "Average is: %d\n", average( -1 ) );
}
/* Returns the average of a variable list of integers. */
int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;
va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}
Default values in arguments.
When declaring a function we can specify a default value for each parameter. This value will be used if that parameter is left blank when calling to the function. To do that we simply have to assign a value to the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is stepped on and the passed value is used. For example:
Code:
// default values in functions
#include <iostream.h>
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
As we can see in the body of the program there are two calls to the function divide. In the first one:
divide (12)
we have only specified one argument, but the function divide allows up to two. So the function divide has assumed that the second parameter is 2 since that is what we have specified to happen if this parameter is lacking (notice the function declaration, which finishes with int b=2). Therefore the result of this function call is 6 (12/2).
In the second call:
divide (20,4)
there are two parameters, so the default assignation (int b=2) is stepped on by the passed parameter, that is 4, making the result equal to 5 (20/4).
you can find the above here
hope that helps..
God Bless
R. Thomas
"Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
"Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18
Joseph_R_Thomas. this is exactly what I done and compiler tells me that the function do not except the number of parameters that i supplied.
maybe it is because I am using const CString, and not int?
here is the exact code:
and here i call the func:
FileLink("bla","blabla");
FileLink("bla","blabla","blablabla);
irona20. this method seems to be intresting i didn't know her before, but i thinkk this is good when you have alot of parameters and the number of parameters are unknown. in my case i know exaxtly how many parameters I have, so i dont need to enter to a loop.
if no other option will be found I think I will use it.
This line doesn't even compile, and the error isn't what you described. It is illegal to convert a CString to an "int", and that is what NULL is -- an integer. That is the error that the compiler flags, not that the number of parameters don't match.
error C2440: 'default argument' : cannot convert from 'const int' to 'const class CString'
No constructor could take the source type, or constructor overload resolution was ambiguous
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 25th, 2003 at 02:02 PM.
Paul McKenzie. you are right. the line does not compile, so i have changed it into:
BOOL FileLink(const CString strPathObj,const CString strPathLink,
const CString strArg="Text")
{
return TRUE;
}
but still the line: FileLink("bla","blabla"); does not compiled because the number of parameters are unadequate:
error C2660: 'FileLink' : function does not take 2 parameters
Andreas Masur. I dont need an empty string. I need to post 2 parameters to a function that takes 3 parameters.
I changed the code into:
BOOL FileLink(const CString strPathObj,const CString strPathLink,
const CString strArg="Text")
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.