如何在C或C++中使用未命名的函数参数

zed*_*d91 9 c++ arguments function

我如何使用声明的函数参数

void f(double)
{
    /**/
}
Run Code Online (Sandbox Code Playgroud)

如果有可能?

And*_*bel 41

我希望一个例子可以提供一些帮助:

// Declaration, saying there is a function f accepting a double.
void f(double);

// Declaration, saying there is a function g accepting a double.
void g(double);

// ... possibly other code making use of g() ... 

// Implementation using the parameter - this is the "normal" way to use it. In
// the function the parameter is used and thus must be given a name to be able
// to reference it. This is still the same function g(double) that was declared
// above. The name of the variable is not part of the function signature.
void g(double d)
{
  // This call is possible, thanks to the declaration above, even though
  // the function definition is further down.
  f(d);
}

// Function having the f(double) signature, which does not make use of 
// its parameter. If the parameter had a name, it would give an 
// "unused variable" compiler warning.
void f(double)
{
  cout << "Not implemented yet.\n";
}
Run Code Online (Sandbox Code Playgroud)

  • @ zed91 - 您应该将帮助您解决问题的答案标记为"已接受".无论投票得分如何,您都可以将任何答案标记为已接受.始终接受您的问题的答案被认为是礼貌的.它也会让你获得+2的声望和答案的作者+15. (2认同)