2013-09-26 98 views
8

我有一个C++函数,它有5个参数,所有参数都有默认值。如果我传入前三个参数,程序将为最后两个参数分配一个默认值。有什么办法可以传递3个参数,并在中间跳过一个参数,给出值,说第一个,第二个和第五个参数?在C++函数中跳过一些参数?

+11

简答:第 –

+10

有解决方法。但是如果可以的话,那就是超载。 – jrok

回答

0

不,这是不可能的。
但是我建议你应该使用参数的数据类型数组来代替你实现的场景。 您也可以重载。如果参数的数据类型不同,您应该定义一个类,它具有所需参数作为成员。传递该类的对象。它不仅可以解决您的问题,而且还可以从可维护性的角度来推荐。

5

不是直接的,但你也许能够做一些事情的std ::绑定:

int func(int arg1 = 0, int arg2 = 0, int arg3 = 0); 

// elsewhere... 
using std::bind; 
using std::placeholders::_1; 
auto f = bind(func, 0, _1, 0); 

int result = f(3); // Call func(0, 3, 0); 

缺点当然是您要重新指定默认参数。我相信别人会有更聪明的解决方案,但如果你真的很绝望,这可能会奏效。

1

使用经典的5参数函数,没有办法给它只有3或4.你只能用默认参数写入3或4,但最后你将得到一个带有5个参数的函数调用。

如果存在几个相同类型的参数,那么系统也存在问题。 例如,如果您有foo(int a=4,int b=5)并致电foo(10),您如何知道您要拨打foo(10,5)foo(4,10)

随着C++ 11元组和Named parameters idiom,你可以作弊一点点。

#include <iostream> 
#include <functional> 
#include <tuple> 
#include <string> 

struct f_ 
{ 
    private: 

    typedef std::tuple<int,int,double> Args; 

    //default arguments 
    static constexpr const Args defaults = std::make_tuple(10,52,0.5); 
    Args args; 
    public : 
    f_():args(defaults) 
    {} 

    template <int n,class T> f_& setArg(T&& t) 
    { 
     std::get<n>(args) = t; 
     return *this; 
    } 

    void operator()() 
    { 
     return (*this)(std::move(args)); 
    } 

    void operator()(Args&& a) 
    { 
     int n1=std::get<0>(a); 
     int n2=std::get<1>(a); 
     double n3=std::get<2>(a); 

     std::cout<<n1<<" "<<n2<<" "<<n3<<std::endl; 
    } 
}; 
#define set(n,v) setArg<n>((v)) 
int main() 
{ 
    //f_().set<1>(42).set<3>("foo")(); 
    f_().setArg<1>(42)(); //without Macro 
    f_().set(0,666).set(1,42)(); //with Macro 
    f_()(); //without any parameters 
    f_()(std::forward_as_tuple(-21,-100,3.14)); //direct call 
} 

的另一种方法是使用std ::绑定描述there

-2

也许这是东西,你可能会寻找,只是一个解决办法!

/* 
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; 
} 
+1

我不能去全局静态变量来选择方法参数。没有人应该使用这种方法。 – Brannon