Can I set a default argument from a previous argument?

后端 未结 7 1776
终归单人心
终归单人心 2020-11-27 20:09

Is it possible to use previous arguments in a functions parameter list as the default value for later arguments in the parameter list? For instance,

void f(         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 20:34

    As a potential workaround, you could do:

    const int defaultValue = -999; // or something similar
    
    void f( int a, int b = defaultValue, int c = defaultValue )
    {
        if (b == defaultValue) { b = a; }
        if (c == defaultValue) { c = b; }
    
        //...
    }
    

提交回复
热议问题