2013-09-26 39 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