这可能是您可能正在寻找的东西,只是一种解决方法!
/*
Function f() is called by passing 2 arguments. So to make sure that these 2 arguments are treated as
first and third, whereas the second and the fourth are taken as defaults:
SOLUTION 1 : using recursive call
*/
#include <iostream>
using namespace std;
void f( int = 10,int = 20, int = 30, int = 40);
static int tempb;
static int flag = 1;
int main()
{
cout << "calling function \n";
//f();
f(12,39);
}
void f( int a,int b,int c,int d )
{
//static int flag = 1;
//f();
if( flag == 1 )
{
--flag;
f(); //recursive call to intialize the variables a,b,c,d as per the prototype
c = b;
b = tempb;
//cout << c;
}
else
{
tempb = b;
return;
}
cout << endl <<"a = " << a << endl << "b = "<< b << endl << "c = " << c << endl << "d = " << d << endl;
}
以下是另一种解决方法,可能也有帮助!
/*
Function f() is called by passing 2 arguments. So to make sure that these 2 arguments are treated as
first and third, whereas the second and the fourth are taken as defaults:
SOLUTION 2 : using static variable
*/
#include <iostream>
using namespace std;
void f( int = 10,int = 20, int = 30, int = 40);
static int tempb;
int main()
{
f();
f(12,39);
}
void f( int a,int b,int c,int d)
{
static int flag = 1;
if( flag == 1 )
{
--flag;
tempb = b;
return;
}
else
{
c = b;
b = tempb;
}
cout << "a = " << a << endl << "b = " << b << endl << "c = " << c << endl << "d = " << d;
}