Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a preceding function parameter to its default in c++?

Apologies if this basic question has already been answered. What would I put inside the brackets of print() so that the first parameter is left to the default value but the following parameters are given new values of 1 and 2? I know I can literally put 0 in there but is there a way for it go to a default?

#include<iostream>

using namespace std;

void printer(int a=0, int b=0, int c=0){
 cout << a << endl;
 cout << b << endl;
 cout << c << endl;
}

int main(){

//leave a=0 and replace both b and c 
printer(/*?*/,1,2);

 return 0;
}
like image 311
user1905552 Avatar asked Dec 22 '25 10:12

user1905552


2 Answers

You cannot do that, it's not allowed. Only right most parameters could be omitted.

like image 108
TieDad Avatar answered Dec 24 '25 01:12

TieDad


Default parameter list is right associative. So its not possible to ommit first parameter list.

like image 26
shivakumar Avatar answered Dec 24 '25 01:12

shivakumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!