English 中文(简体)
C++ 履历表
原标题:C++ destructor & function call order

附录一如下:

Foo foo;
....
return bar();

如今,C++标准是否保证了我,在禁oo前将叫酒吧:~Foo()? 还是这种汇编者/实施者的选择?

感谢!

最佳回答

它得到保障的行为。 实际执行情况如下:

0: enter block (scope)
1: Foo::Foo()
2. evaluation of bar(); as expression in return statement
3. save result of the expression as value returned from function
4. finalize return statement to leave function to its caller (request exit from current scope)
5: exit block (scope) with call to  Foo::~Foo()

此处指标准的一些参考资料:

  • What program execution guarantees, generally

1.9 方案执行

10 An instance of each object with automatic storage duration (3.7.2) is associated with each entry into its block.

  • The foo is of automatic storage duration 以及:

3.7.2 自动储存期

1 Local objects explicitly declared auto or register or not explicitly declared static or extern have automatic storage duration. The storage for these objects lasts until the block in which they are created exits.

  • What is actual effect of return statement

6.6.3 返回声明

2(......)该表述的价值被退回职能召集人

以及

6.6 发言(返回属于跳跃发言)

2 On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2)

  • What guarantees that the effect occurs

6.7 声明

2 Variables with automatic storage duration declared in the block are destroyed on exit from the block

以及

12.4 驾驶员

10 Destructors are invoked implicitly (1) for a constructed object with static storage duration (3.7.1) at program termination (3.6.3), (2) for a constructed object with automatic storage duration (3.7.2) when the block in which the object is created exits (6.7)

It is not easy to grasp single idea form details scattered around all the C++ st以及ard. Hopefully, quick overview will help you to make such analysis yourself too.

问题回答

是的,将向 f的主子提出条条。

The standard says: 6.6: "On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration."

直到填写返回报表为止,范围一直没有。

必须在清理含有Foo的 st框架之前对 calling( bar)的结果进行评估,因此,在Foo:~Foo(Foo)之前,会叫作是的。

Objects destruct何时离开>。

离开范围,但可在执行<条码>/条码>之前返回。 Ergo,bar()。

只是想到的是,如果是<条码>? 这只是has去工作,如果销毁令有不同,则这sil笑不清,取决于你是否把这一命令当作理由。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签