Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ Default paramaters: is it possible to override a default parameter without overriding earlier default parameters

Tags:

c++

function

I have a function:

int function(int a, int b = 1, int c = 2){
    return a+b+c;
}

I want to set the value of the "c" variable to 3, but don't want to set the value of "b"

In a language like python I can do this:

function(23,c=3)

However in c++ I cant find a way to do something like that. All examples I could find involved setting the value of "b" before the value of "c", like this:

function(23,1,3);

How can I set the value of a default parameter directly?

like image 809
abc Avatar asked Dec 04 '25 09:12

abc


1 Answers

This is not possible in C++ (at least not directly). You have the provide all parameters up to the last one you want to provide, and in the order given by the declaration.

like image 162
Baum mit Augen Avatar answered Dec 05 '25 22:12

Baum mit Augen



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!