构造器初始化列表评估顺序 [英] Constructor initialization-list evaluation order

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

问题描述

我有一个构造函数,需要一些参数。我假设他们是按照列出的顺序建造的,但在一种情况下,它们似乎被反向构建,导致中止。当我扭转参数时,程序停止中止。这是我使用的语法的一个例子。事情是,在这种情况下,a_需要在b_之前初始化。你能保证施工的顺序吗?

I have a constructor that takes some arguments. I had assumed that they were constructed in the order listed, but in one case it appears they were being constructed in reverse resulting in an abort. When I reversed the arguments the program stopped aborting. This is an example of the syntax I'm using. The thing is, a_ needs to be initialized before b_ in this case. Can you guarantee the order of construction?

例如

class A
{
  public:
    A(OtherClass o, string x, int y) :
      a_(o), b_(a_, x, y) { }

    OtherClass a_;
    AnotherClass b_;
};


推荐答案

这取决于成员变量声明的顺序类。因此, a _ 将是第一个,然后 b _ 将是您示例中的第二个。

It depends on the order of member variable declaration in the class. So a_ will be the first one, then b_ will be the second one in your example.

这篇关于构造器初始化列表评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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