C++ FAQ 中的一个问题讨论了在函数中声明本地对象的正确方法:http: //www.parashift.com/c++-faq/fn-decl-vs-obj-instantiation.html
给出FAQ中的示例:
class Bar {
public:
Bar();
};
class Foo {
public:
Foo(Bar const& b);
void blah();
};
void yourCode()
{
Foo x(Bar()); // error
}
在 VS2012 中,错误是Foo x(Bar (__cdecl *)(void))': prototyped function not called (was a variable definition intended?)
有人可以解释一下为什么这个声明会出错吗?(C++ FAQ 解释太模糊了)。
Foo
并且Bar
在 的正文中都是可见的yourCode()
,并且Foo x(Bar());
是我声明类型对象的一种方式Foo
。