问:对象的构造顺序 [英] Q: Order of construction for objects

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

问题描述




我知道全局

对象的构造顺序未指定。但我处于这样一种情况,我需要保证,b
保证,一个对象是在别人之前创建的(不是所有其他对象,

只是指定类型的所有对象)。我提出了以下

解决方案,但我不确定,如果它确实解决了我的问题:


类首先

{

};


A级

{

静态首先first_obj;

};


int main()

{

A mya;

}


类A(进一步实现省略)将依赖于类首先完成的一些初始化。是否保证这个

初始化将全部完成,然后才使用A?


谢谢!

-

jb


(rot13中的回复地址,先解读)

解决方案

Jakob Bieling写道:



我知道全局
对象的构造顺序是未指定的。


不完全。在一个翻译单元中,它是明确定义的。

但我处于一种情况,我需要保证,在其他人之前创建一个对象(不是所有其他对象,只是所有对象)指定
类型的对象。)




执行此操作的典型方法是使用单例模式。类似

这样的东西:


班级优先

{

静态优先&实例()

{

静态第一个inst;

返回inst;

}


私人:

first(){}


//防止复制

first(first&) ;

void operator =(first&);

};


然后你只通过实例使用对象()会员功能。这个

保证在第一次需要

时创建对象。


< blockquote>Rolf Magnus < RA ****** @ t-online.de>在消息中写道

news:d7 ************* @ news.t-online.com ...

Jakob Bieling写道:



我知道全局
对象的构造顺序是未指定的。



不完全。在一个翻译单元中,它是明确定义的。

但我处于这样的情况,我需要保证,之前创建了一个对象
其他(不是所有其他的,只是指定
类型的所有对象)。



典型的方法是使用单例模式。这个类似的东西:

上课第一次
{静态第一&实例()
{静态第一个;
返回inst;
}
私人:
first(){}

//防止复制
首先(第一个&);
void operator =(first&);
};

然后你只使用通过instance()成员函数对象。
这保证了对象在第一次需要时创建。



更确切地说,我不需要对象本身..它只有

包含一个ctor和一个dtor,它负责初始化和

清理。并且A类需要进行初始化。


我可以在启动之前进行初始化,但是我希望
能够实现自动化。 />

感谢您的帮助!

-

jb


(回复地址in rot13,首先解读)


" Jakob Bieling" < AR **************** @ rot13.com>在消息中写道

news:d7 ************* @ news.t-online.com


<我知道,全局
对象的构造顺序是未指定的。


如果它们位于不同的翻译单元中,则未指定。如果它们在

相同的翻译单元中,则构造与

定义的顺序相同。标准的第3.6.2 / 1节:


具有静态存储持续时间(3.7.1)的对象应为零初始化

(8.5)在进行任何其他初始化之前。零初始化和使用常量表达式的
初始化统称为static

初始化;所有其他初始化是动态初始化。对象

的POD类型(3.9)静态存储持续时间初始化为常量
表达式(5.19)应在任何动态初始化之前初始化

地点。具有静态存储持续时间的对象在命名空间作用域中定义

在同一个翻译单元中并动态初始化应按照其定义出现在
中的顺序初始化为

翻译单元。

但是我处于这样的情况,我需要保证,在其他人之前创建一个对象(不是所有其他对象,
只是指定类型的所有对象)。我想出了以下的解决方案,但我不确定,如果它确实解决了我的问题:

上课第一页
{
};

A级
{静静第一_obj;
};



这只声明first_obj,你在哪里定义它?

int main()
{
一个mya ;
}

A类(进一步实现省略)将依赖于类首先完成的一些初始化。是否保证在我使用A之前完成这个
初始化?



标准的第3.6.2 / 3节:


"实现定义是否在命名空间作用域对象的动态初始化

(8.5,9.4,12.1,12.6.1)之前完成了

首次发表声明。如果在main的第一个语句之后初始化延迟到

时间的某个时间点,它应该在第一次使用之前发生

在同一个翻译中定义的任何函数或对象单位作为对象

初始化.31)[例如:

//?文件1?

#include" a.h"

#include" b.h"

B b;

A :: A(){

b.Use();

}


//?文件2?

#include" a.h"

A a;


//?文件3?

#include" a.h"

#include" b.h"

extern A a;

extern B b;

int main(){

a.Use();

b.Use();

}

实现定义是否在输入
main之前初始化a或b,或者初始化是否延迟到a是第一个

用于主要。特别是,如果a在main输入之前被初始化,那么

不能保证在初始化之前b将被初始化,即

初始化a,即在A :: A被调用之前。但是,如果a在第一个main语句之后的某个时刻初始化为a / b
,则b将在A :: A中使用之前初始化为b $ b。 ]


第9.4.2 / 7节:


静态数据成员的初始化和销毁​​与非本地成员完全相同/>
个对象(3.6.2,3.6.3)。"


从这一切,我们可以得出结论:初始化


静态首先first_obj;


应在首次使用[在main()]之前发生任何函数或对象

在与要初始化的对象相同的翻译单元[即

first_obj]。因此,如果first_obj和A在同一个翻译单元中定义,则必须在main()中首次使用A之前初始化
first_obj。至少

这就是我的看法。


-

John Carson


Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified. But I am in a situation, where I need to
guarantee, that one object is created before others (not all others,
just all objects of a specified type). I came up with the following
solution, but I am not sure, if it does solve my problem:

class first
{
};

class A
{
static first first_obj;
};

int main ()
{
A mya;
}

class A (further implementation omitted) will rely on some
initialization done by class first. Is it guaranteed that this
initialization will be all done, before I use A?

thanks!
--
jb

(reply address in rot13, unscramble first)

解决方案

Jakob Bieling wrote:

Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified.
Not entirely. Within one translation unit, it is well-defined.
But I am in a situation, where I need to guarantee, that one object is
created before others (not all others, just all objects of a specified
type).



The typical way to do this is by using the singleton pattern. Something like
this:

class first
{
static first& instance()
{
static first inst;
return inst;
}

private:
first() {}

// prevent copying
first(first&);
void operator=(first&);
};

Then you only use the object through the instance() member function. This
guarantees that the object gets created in the moment it is needed the
first time.


"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d7*************@news.t-online.com...

Jakob Bieling wrote:

Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified.



Not entirely. Within one translation unit, it is well-defined.

But I am in a situation, where I need to guarantee, that one object
is
created before others (not all others, just all objects of a
specified
type).



The typical way to do this is by using the singleton pattern.
Something like
this:

class first
{
static first& instance()
{
static first inst;
return inst;
}

private:
first() {}

// prevent copying
first(first&);
void operator=(first&);
};

Then you only use the object through the instance() member function.
This
guarantees that the object gets created in the moment it is needed the
first time.


To be more exact, I will not need the object itself .. it only
contains a ctor and a dtor, which take care of initialization and
cleanup. And that initialization is needed for class A.

I could do the initialization at startup before anything else, but I
would like to automate this.

Thanks for the help!
--
jb

(reply address in rot13, unscramble first)


"Jakob Bieling" <ar****************@rot13.com> wrote in message
news:d7*************@news.t-online.com

Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified.
It is unspecified if they are in different translation units. If they are in
the same translation unit, then construction is in the same order as
definition. Section 3.6.2/1 of the standard:

"Objects with static storage duration (3.7.1) shall be zero-initialized
(8.5) before any other initialization takes place. Zero-initialization and
initialization with a constant expression are collectively called static
initialization; all other initialization is dynamic initialization. Objects
of POD types (3.9) with static storage duration initialized with constant
expressions (5.19) shall be initialized before any dynamic initialization
takes place. Objects with static storage duration defined in namespace scope
in the same translation unit and dynamically initialized shall be
initialized in the order in which their definition appears in the
translation unit."
But I am in a situation, where I need to
guarantee, that one object is created before others (not all others,
just all objects of a specified type). I came up with the following
solution, but I am not sure, if it does solve my problem:

class first
{
};

class A
{
static first first_obj;
};

This only declares first_obj, where do you define it?
int main ()
{
A mya;
}

class A (further implementation omitted) will rely on some
initialization done by class first. Is it guaranteed that this
initialization will be all done, before I use A?


Section 3.6.2/3 of the standard:

"It is implementation-defined whether or not the dynamic initialization
(8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the
first statement of main. If the initialization is deferred to some point in
time after the first statement of main, it shall occur before the first use
of any function or object defined in the same translation unit as the object
to be initialized.31) [Example:
// ? File 1 ?
#include "a.h"
#include "b.h"
B b;
A::A(){
b.Use();
}

// ? File 2 ?
#include "a.h"
A a;

// ? File 3 ?
#include "a.h"
#include "b.h"
extern A a;
extern B b;
int main() {
a.Use();
b.Use();
}
It is implementation-defined whether either a or b is initialized before
main is entered or whether the initializations are delayed until a is first
used in main. In particular, if a is initialized before main is entered, it
is not guaranteed that b will be initialized before it is used by the
initialization of a, that is, before A::A is called. If, however, a is
initialized at some point after the first statement of main, b will be
initialized prior to its use in A::A. ]"

Section 9.4.2/7:

"Static data members are initialized and destroyed exactly like non-local
objects (3.6.2, 3.6.3)."

From all this, we may conclude: the initialization of

static first first_obj;

"shall occur before the first use [in main()] of any function or object
defined in the same translation unit as the object to be initialized [i.e.,
first_obj]." So if first_obj and A are defined in the same translation unit,
first_obj must be initialised before the first use of A in main(). At least
that is how it appears to me.

--
John Carson


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

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