对象初始化 [英] Object Initialisation

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

问题描述



我刚刚在标准版中阅读了8.5版本,并且我试图理解

的各种初始化。


到目前为止,我认为一个对象要么未初始化(即

包含垃圾),要么默认初始化(即包含

默认值对于那种类型)。


以下是前者的一些例子:


struct MyStruct {


int a;

double b;

void * p;

};


int main()

{

int a; / *包含垃圾* /

双b; / *包含垃圾* /


void * p; / *包含垃圾* /

MyStruct obj; / *所有成员都包含垃圾* /

}

这里有一些后者的例子:


struct MyStruct {


int a;

double b;

void * p;

};

int main()

{

int a = int(); / *包含0 * /


double b = double(); / *包含0.0 * /


typedef void * voidptr;

void * p = voidptr(); / *包含空指针值* /


MyStruct obj = {}; / *每个成员具有与上面相同的值* /

}


标准告诉我有三种初始化:

零初始化

默认初始化

价值初始化

在上面的代码片段中,我要进行什么样的初始化?我认为它是'默认初始化',因为我给对象提供了默认的

值。


什么是零初始化,有人可以给出一个例子吗?


什么是价值初始化,谁能举一个例子呢?

此外,以下课程的初始化时间是什么?


模板< class T>

struct Init {


T obj;


Init():obj(){}

};


-


Frederick Gotham


I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations.

Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}

The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
In my above code snippet, what kind of initialisation am I peforming? I
think it''s "default initialisation", as I''m giving the object its default
value.

What is zero-initialisation, and can anyone give an example of it please?

What is value-initialisation, and can anyone give an example of it please?
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};

--

Frederick Gotham

推荐答案

Frederick Gotham写道:
Frederick Gotham wrote:

我刚刚在标准版中阅读了8.5版本,并且我正在尝试使用

感知不同类型的初始化。


到目前为止,我认为一个对象要么没有初始化

(即包含垃圾),要么默认初始化(i .e。

包含该类型的默认值。


以下是前者的一些例子:


struct MyStruct {


int a;

double b;

void * p;

};


int main()

{

int a; / *包含垃圾* /

双b; / *包含垃圾* /


void * p; / *包含垃圾* /

MyStruct obj; / *所有成员都包含垃圾* /

}


这里有一些后者的例子:


struct MyStruct {


int a;

double b;

void * p;

} ;


int main()

{

int a = int(); / *包含0 * /


double b = double(); / *包含0.0 * /


typedef void * voidptr;

void * p = voidptr(); / *包含空指针值* /


MyStruct obj = {}; / *每个成员具有与上面相同的值* /

}


标准告诉我有三种初始化:


零初始化

默认初始化

价值初始化
I have just been reading 8.5 in the Standard, and am trying to make
sense of the different kinds of initialisations.

Up until now, I thought of an object as either NOT being initialised
(i.e. containing garbage), or being default initialised (i.e.
containing the default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}

The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation



那里是第四种:未初始化。

And there is the fourth kind: uninitialised.


在上面的代码片段中,我要进行什么样的初始化?
In my above code snippet, what kind of initialisation am I peforming?



第一部分,没有。对象是_uninitialised_。在第二个

部分_default_。

In the first part, none. Objects are _uninitialised_. In the second
part the _default_.


我认为它是'默认初始化',因为我给的是对象它的

默认值。
I think it''s "default initialisation", as I''m giving the object its
default value.



对。仅在第二种情况下。

Right. Only in the second case.


什么是零初始化,任何人都可以给出一个例子

好​​吗?
What is zero-initialisation, and can anyone give an example of it
please?



这是POD默认初始化的化身。

That''s the incarnation of the default-initialisation for PODs.


什么是价值 - 初始化,任何人都可以给它一个例子

拜托?
What is value-initialisation, and can anyone give an example of it
please?



int a(5);

int a(5);


此外,以下课程执行什么样的初始化?


模板< class T>

struct Init {


T obj;


Init():obj(){}

};
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};



默认。


V

-

请在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问

Default.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov发布:

Victor Bazarov posted:


>什么是零初始化,任何人都可以给出一个例子好吗
好吗?
>What is zero-initialisation, and can anyone give an example of it
please?



这是POD默认初始化的化身。


That''s the incarnation of the default-initialisation for PODs.



我们发明单词的原因并非如此,我们有一个标签,但是我们可以将它与其他东西区分开来。


这就是为什么我对零初始化感到困惑的原因。比。 default-

初始化。


它们不完全一样吗?如果是这样,为什么给他们不同的名字呢?


你能举一个零对象初始化的例子吗?


在下面的代码中什么样的初始化发生?


struct MyStruct {


int a;

double b;

void * p;

};


int main()

{

MyStruct static obj; / *零或默认? * /

MyStruct static obj = {}; / *零或默认? * /

MyStruct obj = {}; / *零或默认? * /

}


-


Frederick Gotham


The reason we invent words is not so we have a label for something, but so
that we can distinguish it from other things.

That is why I''m confused with "zero-initialsation" Vs. "default-
initialisation".

Are they not exactly the same? If so, why give them different names?

Can you please give an example of zero-initialising an object?

In the following code, what kind of initialisation takes place?

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
MyStruct static obj; /* zero or default? */
MyStruct static obj = {}; /* zero or default? */
MyStruct obj = {}; /* zero or default? */
}

--

Frederick Gotham

Frederick Gotham写道:
Frederick Gotham wrote:

Victor Bazarov发布:

Victor Bazarov posted:


>>什么是零初始化,谁能举一个例子呢
好吗?
>>What is zero-initialisation, and can anyone give an example of it
please?


这是POD默认初始化的化身。


That''s the incarnation of the default-initialisation for PODs.




我们发明单词的原因并非如此,我们有一个标签,

但我们可以区分它来自其他的东西。


这就是为什么我对零首字母缩写感到困惑的原因。比。 default-

初始化。


它们不完全一样吗?



The reason we invent words is not so we have a label for something,
but so that we can distinguish it from other things.

That is why I''m confused with "zero-initialsation" Vs. "default-
initialisation".

Are they not exactly the same?



编号是。对于POD,它们是相同的。对于非POD,它们不是相同的


No. Yes. For PODs they are the same. For non-PODs they are not
the same.


如果是这样,为什么给它们不同的名字?
If so, why give them different names?



因为它们不是,一般情况下。

Because they are not, in general case.


你能举一个零的例子吗?初始化一个对象?
Can you please give an example of zero-initialising an object?



static int a;

void * p(0);

static int a;
void *p(0);


在下面的代码中,进行了什么样的初始化?


struct MyStruct {


int a;

双b;

void * p;

};


int main()

{

MyStruct static obj; / *零或默认? * /
In the following code, what kind of initialisation takes place?

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
MyStruct static obj; /* zero or default? */



Zero。

Zero.


MyStruct static obj = {}; / *零或默认? * /
MyStruct static obj = {}; /* zero or default? */



首先为零,然后是默认值(在这种情况下是多余的)。

First zero, then default (which in this case superfluous).


MyStruct obj = {}; / *零或默认? * /
MyStruct obj = {}; /* zero or default? */



默认值,POD为零。

Default, which for POD is zero-.


}
}



V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


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

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