什么是int()调用? [英] What is an int() Called?

查看:291
本文介绍了什么是int()调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始类型没有构造函数。例如,当我调用 Foo()时, _bar 未初始化为0:

  class Foo {
int _bar;
};
不是构造函数。



在这个例子中,我会说 i is:(construct?initialized?fooed?)

  for(int i {}; i <13; ++ i)

Loki Astari 提及这里表示该技术有某种名称。



编辑以响应 Mike Seymour

  #include< iostream& 

using namespace std;

class Foo {
int _bar;
public:
void printBar(){cout< _bar<< endl; }
};

int main()
{
Foo foo;

foo.printBar();

Foo()。printBar();

return 0;
}

在Visual Studio 2013上运行此代码会产生:


3382592

3382592


有趣的是gcc 4.8.1产生:


134514651

0



解决方案


原始类型没有构造函数。


没错。


当我调用 Foo()



$ b时,此栏不会初始化为0它是。 Foo()指定值初始化,对于没有用户提供的构造函数的类,这意味着它在初始化成员之前是零初始化的。因此, _bar 结尾的值为零。 (虽然,如评论中所述,一个流行的编译器不能正确地初始化这样的类。)



如果你使用default-初始化。你不能用临时的;但是声明的变量 Foo f; 或者由 new F 创建的对象将被默认初始化。如果类有一个用户提供的默认构造函数,那么它也不会被初始化,而且它不会被初始化。构造函数没有专门初始化 _bar 。再次,它将是默认初始化,没有效果。


显然,int()不是一个构造函数。


作为一个表达式,它是一个值初始化的临时类型 int



在语法上,它是一个显式类型转换(功能符号)的特殊情况;但是对于除了类型转换之外的任何东西,使用该术语会相当混乱。


在这个例子中,我会说 i is:(constructed?initialized?fooed?)


如果你想要更具体,列表初始化(带有空列表),值初始化或零初始化。


It's been rehashed over and over that primitive types don't have constructors. For example this _bar is not initialized to 0 when I call Foo():

class Foo{
    int _bar;
};

So obviously int() is not a constructor. But what is it's name?

In this example I would say i is: (constructed? initialized? fooed?)

for(int i{}; i < 13; ++i)

Loki Astari mentions here that the technique has some sort of name.

EDIT in response to Mike Seymour:

#include <iostream>

using namespace std;

class Foo{
    int _bar;
public:
    void printBar(){ cout << _bar << endl; }
};

int main()
{
    Foo foo;

    foo.printBar();

    Foo().printBar();

    return 0;
}

Running this code on Visual Studio 2013 yields:

3382592
3382592

Interestingly on gcc 4.8.1 yields:

134514651
0

解决方案

It's been rehashed over and over that primitive types don't have constructors.

That's right.

For example this bar is not initialized to 0 when I call Foo()

Yes it is. Foo() specifies value-initialisation which, for class like this with no user-provided constructor, means it's zero-initialised before initialising its members. So _bar ends up with the value zero. (Although, as noted in the comments, one popular compiler doesn't correctly value-initialise such classes.)

It would not be initialised if you were to use default-initialisation instead. You can't do that with a temporary; but a declared variable Foo f; or an object by new F will be default-initialised. Default-initialisation of primitive types does nothing, leaving them with an indeterminate value.

It would also not be initialised if the class had a user-provided default constructor, and that constructor didn't specifically initialise _bar. Again, it would be default-initialised, with no effect.

So obviously int() is not a constructor. But what is it's name?

As an expression, it's a value-initialised temporary of type int.

Syntactically, it's a special case of an "explicit type conversion (functional notation)"; but it would be rather confusing to use that term for anything other than a type conversion.

In this example I would say i is: (constructed? initialized? fooed?)

Initialised. List-initialised (with an empty list), value-initialised, or zero-initialised, if you want to be more specific.

这篇关于什么是int()调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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