mmnormyle
mmnormyle

Reputation: 883

In c++, how can I pass all of current function's arguments to another function?

Is there an expression in c++ that allows me to pass all of the current function's arguments, in order, to another function? See the example below.

int bar(int a, char* b, double c)
{

}

int foo(int a, char* b, double c)
{
    bar({what goes here?});    // Equivalent to bar(a, b, c);
}

Upvotes: 4

Views: 818

Answers (1)

scohe001
scohe001

Reputation: 15446

I read a book on clean code awhile ago that advocated for a max of 2 function arguments. 3 was only for extenuating circumstances and 4 was a heinous crime.

If you're going to be passing a, b and c around together often, clearly they mean something together. So in a case like this, the book would suggest making a struct to hold that information:

struct Abc {
    int a;
    char *b;
    double c;
};

int bar(Abc abc)
{

}

int foo(Abc abc)
{
    bar(abc);
}

This way you don't punish yourself for longer, more descriptive names, and you can add another informative name for what all three of those values mean together.

Upvotes: 0

Related Questions