2

I have a constructor as below:

Foo::Foo(std::unique_ptr<Bar> bar)
    : left(bar->getLeft()),
      right(bar->getRight()),
      mBar(std::move(bar)) {}

And the program crashed due to bar in left(bar->getLeft()) is nullptr. If I use left(mBar->getLeft()), it works fine.

So I wondered if there's any sequence point in member initial list? If not, how to know for sure if the move statement happened or not? In another word: in this case, when to use argument and when to use member?

  • Can you show the order of data members in your class definition? – lubgr Jul 10 '19 at 10:02
  • [This constructor and initializer list reference](https://en.cppreference.com/w/cpp/language/initializer_list) could be helpful, especially the section about [initialization order](http://en.cppreference.com/w/cpp/language/initializer_list#Initialization_order). – Some programmer dude Jul 10 '19 at 10:05
  • This question is duplicated, thanks all. –  Jul 10 '19 at 10:05

0 Answers0