2013-09-26 33 views
8

Mam funkcję C++, która ma 5 argumentów, z których wszystkie mają wartości domyślne. Jeśli przekażę pierwsze trzy argumenty, program przypisze wartość domyślną do dwóch ostatnich argumentów. Czy istnieje sposób przekazywania 3 argumentów i pomijania jednego w środku, dając wartości do powiedzenia, argumenty pierwszy, drugi i piąty?Pomiń niektóre argumenty w funkcji C++?

+11

Krótka odpowiedź: nr –

+10

Istnieją obejścia. Ale jeśli możesz, przeładuj zamiast tego. – jrok

Odpowiedz

0

Nie, nie jest to możliwe.
Proponuję jednak, abyś zamiast tego podał , używając tablicy parametrów typu, aby uzyskać podany scenariusz. Możesz również przeciążać. Jeśli typy danych różnią się, powinieneś zdefiniować klasę, która ma wymagane parametry jako członkowie. Przekaż obiekt tej klasy. To nie tylko rozwiąże Twój problem, ale także jest zalecane z punktu widzenia łatwości konserwacji.

5

Nie bezpośrednio, ale może być w stanie coś zrobić z std :: wiążą:

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); 

Minusem jest oczywiście, że są ponownie określając parametry domyślne. Jestem pewien, że ktoś inny przyjdzie z bardziej sprytnym rozwiązaniem, ale może to zadziałać, jeśli jesteś naprawdę zdesperowany.

1

Przy klasycznej funkcji 5 argumentów, nie ma możliwości nadania jej tylko 3 lub 4. Można pisać tylko 3 lub 4 z domyślnymi argumentami, ale na końcu otrzymasz wywołanie funkcji z 5 argumentami.

Występują również problemy z systemem, jeśli istnieje kilka parametrów tego samego typu. Na przykład, jeśli masz foo(int a=4,int b=5) i dzwonisz pod numer foo(10), to skąd wiesz, że chcesz zadzwonić pod numer foo(10,5) lub foo(4,10)?

Z krotkami C++ 11 i Named parameters idiom można oszukać go trochę.

#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 
} 

Alternatywną metodą jest użycie std :: wiążą opisanym there

-2

Prawdopodobnie jest to coś, co może być szukasz, tylko obejście!

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

Oto kolejne obejście problemu, które może pomóc!

/* 
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

Nie mogę wybrać globalnej zmiennej statycznej, aby wybrać parametry metody. Nikt nie powinien stosować tego podejścia. – Brannon

Powiązane problemy