为什么在C ++中调用原始类型的构造函数合法? [英] Why is it legal in C++ to call a constructor of a primitive type?

查看:44
本文介绍了为什么在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 是函数声明!

这篇关于为什么在C ++中调用原始类型的构造函数合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆