Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call function that has default arguments 3

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hi,
If I have a function prototype like this:
Code:
int doIt(int value1, int value2 = 0, int value3 = 0);

Is it possible to call the function and pass only the first and last parameters, and let the middle one default?

My books do not mention this scenario at all. They only mention this:
Code:
doIt(1);
doIt(1,2,3);


thanks,

Barry
 
Hi Barry.
I was interested to find out what happens here, so I wrote a program to try and show different scenarios:
Code:
#include <stdio.h>
int doIt (int value1, int value2 = 0, int value3 = 0);

int main(void)
{
	doIt (1, 2, 3);
	doIt (1, 2);
	doIt (1);

	doIt (1, 0, 3);
	return (1);
}

int doIt (int value1, int value2, int value3)
{
	int total;

	total = (value1 + value2 + value3);
	printf(&quot;doIt total = %d\n&quot;, total);
	return (1);
}
The code just prints out the sum of all the values.
I have tried three tests for truncating the parameter lists. The last test was the only way I could get it to recognize just the first and third parameters, this was by having the default value passed in the function ('0').
Hmmm...I guess there should be a way of doing it, but I can't figure it out. I hoped to be helpful, but I might have just been confusing. Sorry.

Adonai :)
 
No, not confusing... This is exactly what I tried. Basically I ran into this when attempting to use a CFileDialog. The constructor only requires the first parameter. The rest have defaults. But I wanted to pass the lst and 5th parameters and leave the rest to default. I could not find a way to do this, so I ended up passing all the parameters and just passing whatever the docs say are the defaults. Not exactly what I wanted, but if anyone knows of how to do this (if it's possible) I would love to hear.


thanks,

Barry
 
Barry,
I know in Visual Basic u can do this to avoid passing the 2nd parameter:
doIt(1,,3); // This passes 1st and 3rd params
If u do this in c++: doIt(1,3);
then the 1st and 2nd params get set.
but i dunno bout c++.
Try it!
Kunal.
 
Yep. I did try that, but C++ just gives a syntax error.


Barry
 
Barry,
I just looked at the docs for CFileDialog(), and read this:

&quot;You can use CFileDialog “as is” with the constructor provided, or you can derive your own dialog class from CFileDialog and write a constructor to suit your needs. In either case, these dialog boxes will behave like standard Microsoft Foundation class dialog boxes because they are derived from the CCommonDialog class.&quot;

To pass only some of the arguments you can write a different constructor. It seems too much of a chore though, and I would stick to just supplying the default values as parameters to their default (and working) constructor.

Adonai :)

 
There's no way to do what you're suggesting directly.

You could, of course, just write a wrapper function that takes the first and third arguments, then calls the real function using those arguments and the default.

It won't be much help if the function has already been written, but you might check out the &quot;named parameter idiom&quot; by doing a Google search.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top