c++ - Initialization list to constructor -
i'm working on assignment in school supposed make constructor our own list-class takes initialization list argument.
this want able do:
list ourlist {1, 2, 3};
this have far:
list::list(std::initializer_list<int> il) { head_ = copy(il.begin(), il.end()); } list_node* list::copy(std::initializer_list<int>::iterator begin, std::initializer_list<int>::iterator end) { if(begin == end) return nullptr; list_node* new_list = new list_node(*begin); list_node* node = copy(begin++, end); new_list->next_ = node; return new_list; }
in humble opinion, should work great. however, when try initialization (list list {1,2,3};
) seg-fault. please explain i'm doing wrong here?
list_node* node = copy(begin++, end);
this call copy
again same arguments, recursing forever , never completing.
you should have been able tell using debugger see crashed, , have seen there hundreds of calls list::copy
, not 3 calls expected.
you want ++begin
not begin++
Comments
Post a Comment