相关疑难解决方法(0)

本地函数声明有用吗?

像我这样的大多数C++程序员在某些方面犯了以下错误:

class C { /*...*/ };

int main() {
  C c();     // declares a function c taking no arguments returning a C,
             // not, as intended by most, an object c of type C initialized
             // using the default constructor.
  c.foo();   // compiler complains here.

  //...
}
Run Code Online (Sandbox Code Playgroud)

现在虽然错误很明显,一旦你知道它我想知道这种本地函数声明是否有任何明智的用途,除了你可以这样做 - 特别是因为没有办法在同一个函数中定义这样的本地函数块; 你必须在其他地方定义它.

我认为Java风格的本地类是一个非常好的功能,我倾向于经常使用,特别是匿名排序.甚至本地C++类(可以具有内联定义的成员函数)也有一些用处.但是这个没有定义的本地函数声明对我来说似乎很尴尬.它只是一个C-legacy还是有一些我不知道的更深层次的用例?

编辑对于非信徒:C c()不是一个函数指针声明.

这个计划

int main()
{
  void g();
  cout << "Hello ";
  g();
  return 0;
}

void g()
{
  cout << …
Run Code Online (Sandbox Code Playgroud)

c++ function local definition

19
推荐指数
2
解决办法
1万
查看次数

标签 统计

c++ ×1

definition ×1

function ×1

local ×1