为什么在C ++中调用原始类型的构造函数合法?
问题描述:
为什么以下代码在C ++中合法?
Why is the following code legal in C++?
bool a(false);
我的意思是 T a(VALUE)
应该调用构造函数,对吗?我想它没有被解析为函数声明。但是 bool
是普通类型,没有构造函数。还是呢?
I mean, the T a(VALUE)
should call constructor, right? I suppose it's not parsed as function declaration. But bool
is plain type, it doesn't have constructor. Or does it?
如果相关,我正在使用Visual Studio 2012。
I'm using Visual Studio 2012 if it's relevant.
答
这只是初始化POD类型的有效语法,并且具有与构造函数(甚至是复制构造函数)类似的行为。
That is just a valid syntax to initialize POD types and have a similar behavior to a constructor (or even a copy constructor for that matter).
例如,以下内容将是有效的:
For example, the following would be valid:
bool a(false);
bool b(a);
bool c = bool(); // initializes to false
需要注意的一件有趣的事是
One interesting thing to note is that in
int main(int argc, const char *argv[])
{
bool f();
return 0;
}
f
是函数声明!