How can I get all values separated from initializer_list?

615 views Asked by At

I have the following constructor:

JSON(initializer_list<myType> myList){

    if (end == NULL) {
        for(int i=0;i < myList.size() ; i++){
            end = myList(i); /* fix this */
        }

    }

}

end is a struct myType and myType has a value struct myType *nextthat points to the next struct myType and I want to get every individual myType from the initializer_list so that I can put them all manually. When I write end = myList(i); it is throwing an error, what is the correct way to get every individual value ?

1

There are 1 answers

0
Bo Persson On

You have to copy the objects out of the initializer_list, and you cannot index into it (because it is not designed to be used as a proper container).

You can however access the members of the list in a for loop:

JSON(initializer_list<myType> myList){

    if (end == NULL) {
        for(auto& elem : myList){
            end = elem;

            // do something more with end?
        }
    }

}

However, end == NULL might not be the correct way to test for an empty list, but I wouldn't know that without seeing the rest of the coe.