C++是否允许函数在参数不足的情况下调用自身?

scm*_*cmg 0 c++

我知道这个问题听起来很荒谬,但我需要专家的确认,所以请让我解释一下情况:

我正在调试一个C++代码(很长,~5000行),我发现了一些奇怪的东西,我试图简化如下:

myClass.h
class myclass
{
  ...
  void myfun(int p1, int p2, mytype *p3, bool isFirstTime);
  ...
}
=================================================================
myClass.cpp
...
void myclass::myfun(int p1, int p2, mytype *p3, bool isFirstTime)
{
  ...
  if (mycond[y] == false)
  {
    myarr[y] = p1;
    myfun(y, y, p3);   <--- here no bool parameter given     (*)
  }
  ...
}
...
Run Code Online (Sandbox Code Playgroud)

代码可以编译和运行,没有任何错误或警告(用于功能myfun).但由于if-else代码中有许多内容,我不确定(*)在进程中是否实际调用了该行的命令.

所以问题是:这条线是否使用正确?如果正确,请解释我或给我一些关于这种"类型"功能的信息.如果不正确,为什么编译时没有警告或错误?

vso*_*tco 6

如果函数没有默认参数,例如

void f(int x, int y, int z = 0) // we can call inside f(1,2) since z is a default parameter
Run Code Online (Sandbox Code Playgroud)

然后不,它不能用较少的参数调用自己.但是,它可以使用较少的参数调用重载版本:

void f(int x, int y); // one version
void f(int x, int y, int z); // another overload, this can call the first overload f(1,2)
Run Code Online (Sandbox Code Playgroud)

因此,请检查代码中的默认参数或重载版本.此外,默认参数应该放在头文件中,实现文件不必再次指定它们.