1

I am new in c++ i have a question on default argument. If there a function with a following prototype

void f(int=10,int=20,int=30,int=40) 

If this funcion is called by passing 2 arguments to it,how can we make sure that these argumnts are treated as first and third whereas, the second and forth are taken as defaults.

1

7 Answers 7

7

You can't. Arguments to functions match the parameters in order. You can use overloading instead of default arguments like this:

void myFunc(int a,int b,int c,int d); void myFunc(int a,int c) { myFunc(a,20,c,40); } 
Sign up to request clarification or add additional context in comments.

7 Comments

How would you make sure that myFunc(1,2) calls the second function, not the first? Also, what if you wanted to call the first function, passing first two arguments?
If you don't define default values in the prototype of void myFunc(int a,int b,int c,int d) compiler will chose proper function. Otherwise it will give you compiler error.
@Nawaz I didn't put default arguments on any of the parameters. If you want the first one, provide four arguments or two for the second.
@IronMensan: Exactly. Ypu've changed the behaviour. What if I write myFunc(1,2) where the arguments are first two arguments?
@Nawaz you can't have it both ways, myFunc with two arguments has to mean something and the compiler can't read your mind to figure out what. The OP asked for something that can't be done and I suggested something else that might meet his needs.
|
3

That's not how default arguments in C++ work. If you pass two arguments to function f, they will always stand in as the first two arguments, while the last two would be 30 and 40.

In other words, C++ functions only support positional parameters.

1 Comment

...or you could petition the standards committee to add keyword parameters. :)
3

Shouldn't be possible. They'd be treated as the first two.

You can just create a function with a different name, taking two arguments and calling f.

Alternatively, if you want to emulate named arguments, you can use something similar to fluent interfaces. Example:

#include <iostream> using namespace std; int f_impl(int a,int b, int c, int d){ cout << a << " " << b << " " << c << " " << d << endl; return 42; } struct f{ int _a, _b, _c, _d; f() : _a(10), _b(20), _c(30), _d(40){} f& a(int a){ _a = a; return *this;} f& b(int b){ _b = b; return *this;} f& c(int c){ _c = c; return *this;} f& d(int d){ _d = d; return *this;} int operator()(){ return f_impl(_a, _b, _c, _d); } }; #define F(x) (f()x()) int main(){ f().a(100).c(300)(); cout << F(.b(1000).d(4000)) << endl; return 0; } 

Output:

100 20 300 40 10 1000 30 4000 42 

Comments

1

As other people said you can't do that in C++.

But you can create struct/class with four integer members that initialized to values you defined. And you will pass it as a parameter to the function.

Example

struct Param { int a; int b; int c; int d; Param() : a(10), b(20), c(30), d(40) {} void setA(int value) { a = value; } void setB(int value) { a = value; } void setC(int value) { a = value; } void setD(int value) { a = value; } } void f(Param& param) {} Param param; param.setA(67); param.setC(9); f(param); 

Comments

1

The feature you are asking for is called 'Named Parameters'.

Default parameters and Named parameters combined give you lot more options to do things like you suggested but unforunately, C++ doesn't have named parameters. However, some other languages like C# and VB and probably Python have named parameters

Comments

0

Default arguments will be assigned from last if necessary.

http://en.wikipedia.org/wiki/Default_argument

Comments

0

Actually you can have named parameters in C++ with a little help from boost::parameter ( also mentioned in the wiki article on named parameters ).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.