C ++内部类型初始化 [英] C++ intrinsic types initialization

查看:114
本文介绍了C ++内部类型初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

请遵守以下代码:):

Hello.

Please observe the following code :) :

int i1;         // initialized by a "random" value
int i2 = int(); // initialized by 0



你能解释一下第二行的作用吗? :-D

是类型运算符还是构造函数"?

(
发现者:):



Could you explain me the effect of the second line, please ? :-D

Is it an type operator or a "constructor" ?

(
Discovered by :) :

template <typename T>
class holder
{
  T m_value;
public:
  holder() { m_value = T(); }; // ...wow (!)
...
};


)

谢谢!


)

Thank you !

推荐答案

有点像强制转换,并且可以在模板之外使用-请参见
It''s a bit like a cast and works outside of templates - see here[^].


第二个示例也可以是
The second sample can be also
template <typename T>
class holder
{
  T m_value;
public:
  holder() :m_value() {}
...
};



以任何方式……都是标准的C ++:像调用函数一样调用类型会创建一个使用传递的参数构造的临时对象.
因此:



Any way ... that''s standard C++: calling a type like it is a function creates a temporary object constructed with the passed parameters.
Hence:

T myval;
...
myval = T();


只是创建一个使用其默认构造函数显式构造的临时T,然后将其分配给myval;

对于内置构造函数,仅显式调用,而默认构造函数仅将内存归零,因此int() 是设置为0的临时int.

您的第一个示例基于该概念,但有些不同:
尽管看起来很像,但int i2 = x并不是 分配,而是通过副本初始化".

就像是否调用int::int(const int&)来初始化i2,使用由int::int()(默认构造函数)构造的临时int 初始化,其代码执行{ memset(this,0,sizeof(int)); }

就您而言,最好声明一下int i2=0;.

您看到的代码是通用算法的变通方法,适用于通用T类型,以确保即使T是内置类型(C ++),也总是调用T默认构造函数.不要为内置类型初始化自动调用默认构造函数,以保持与C的向后兼容性)


just creates a temporary T constructed explicitly with its default constructor and then assigned to myval;

For built-in constructors are called only explicitly and default constructors just zero the memory, hence int() is a temporary int set to 0.

Your first example is based on that concept, but it is a little different:
despite on what it looks, int i2 = x is not an assignment, but an "initialization by copy".

It''s like if int::int(const int&) is called to initialize i2, using a temporary int constructed with int::int() (the default constructor), whose code does { memset(this,0,sizeof(int)); }

In your case, you''d probably better to simply declare int i2=0;.

The code you see is a workaround for generic algorithms, that works for a generic T type, to be sure that the T default constructor is always called even in the case that T is a built-in type (C++ don''t do automatic call of default constructor for built-in types initialization, to retain backward compatibility with C)


Hmmm ... :)
就像功能一样,
创建一个由0填充的临时实例,
现在就像一个智能"构造函数... :-D

对于给定的代码:
Hmmm... :)
It works like a fuction,
that creates a temp instance filled by 0,
now like a "smart" constructor... :-D

For the given code:
typedef struct {
  int left,
      top,
      right,
      bottom;
} rect;
  
{
  rect r = {0};             // first 
  r = rect();               // second
  memset(&r, 0, sizeof(r)); // third
}


...具有以下顺序:):


...wir have the following sequence :) :

rect r = {0};
00A414E1 C7 45 C4 00 00 00 00 mov         dword ptr [ebp-3Ch],0
00A414E8 33 C0                xor         eax,eax
00A414EA 89 45 C8             mov         dword ptr [ebp-38h],eax
00A414ED 89 45 CC             mov         dword ptr [ebp-34h],eax
00A414F0 89 45 D0             mov         dword ptr [ebp-30h],eax
  r = rect();
00A414F3 33 C0                xor         eax,eax                  // temp begin
00A414F5 89 85 C8 FE FF FF    mov         dword ptr [ebp-138h],eax
00A414FB 89 85 CC FE FF FF    mov         dword ptr [ebp-134h],eax
00A41501 89 85 D0 FE FF FF    mov         dword ptr [ebp-130h],eax
00A41507 89 85 D4 FE FF FF    mov         dword ptr [ebp-12Ch],eax // temp end
00A4150D 8B 8D C8 FE FF FF    mov         ecx,dword ptr [ebp-138h]
00A41513 89 4D C4             mov         dword ptr [ebp-3Ch],ecx
00A41516 8B 95 CC FE FF FF    mov         edx,dword ptr [ebp-134h]
00A4151C 89 55 C8             mov         dword ptr [ebp-38h],edx
00A4151F 8B 85 D0 FE FF FF    mov         eax,dword ptr [ebp-130h]
00A41525 89 45 CC             mov         dword ptr [ebp-34h],eax
00A41528 8B 8D D4 FE FF FF    mov         ecx,dword ptr [ebp-12Ch]
00A4152E 89 4D D0             mov         dword ptr [ebp-30h],ecx
  memset(&r, 0, sizeof(r));
00A41531 6A 10                push        10h
00A41533 6A 00                push        0
00A41535 8D 45 C4             lea         eax,[ebp-3Ch]
00A41538 50                   push        eax
00A41539 E8 ED FC FF FF       call        @ILT+550(_memset) (0A4122Bh)
00A4153E 83 C4 0C             add         esp,0Ch


这是一个调试序列,可能未优化...
...但是它非常有用,我认为...:)


This is a debug-sequence, may be not optimized...
...but it is very useful, I think... :)


这篇关于C ++内部类型初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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