C++,如何在不传递完整参数的情况下调用函数

C++, How to call a function without passing full arguments

我正在查看 qpOASES 项目的源代码。

让我困惑的是如何调用一个函数here:

...
int nWSR = 10;
example.init( H,g,A,lb,ub,lbA,ubA, nWSR );

target 有不同数量的参数:

returnValue QProblem::init( const real_t* const _H, const real_t* const _g, const real_t* const _A,
                            const real_t* const _lb, const real_t* const _ub,
                            const real_t* const _lbA, const real_t* const _ubA,
                            int_t& nWSR, real_t* const cputime,
                            const real_t* const xOpt, const real_t* const yOpt,
                            const Bounds* const guessedBounds, const Constraints* const guessedConstraints,
                            const real_t* const _R
                            )
{
    int_t i;
    int_t nV = getNV( );
    int_t nC = getNC( );

并非所有参数都被传递,但它正常运行,其余参数被传递为 nullptr:



我尝试不完整地向函数发送参数,但它对我不起作用。那么为什么它适用于 qpOASES?

void init(  int a, double* const cputime,
                            const double* const xOpt, const double* const yOpt,
                            const double* const _R
                            )
{
}


int main()
{
    init(0);

    return 0;
}
// test.cpp:12:8: error: too few arguments to function ‘void init(int, double*, const double*, const double*, const double*)’
//   init(0);
//         ^
// test.cpp:2:6: note: declared here
//  void init( int a, double* const cputime,
//       ^

How to call a function without passing full arguments

除了具有默认值的参数外,您必须为所有参数提供值。

void foo(int a, int b, int c = 10) { ... }


foo(10, 20);    // Ok. c gets the default value of 10
foo(10, 20, 30);   // c gets the value of 30

foo(10);        // Not Ok. Value provided for a but not b