We wrote a custom container class that should be able to support rang based for loop, see here: c++ shell. When you run the example you can see that it is working.
Essentially the container is a linked list of pointers to Elements:
LinkedList ll;
ll.push_back(new Element(1));
ll.push_back(new Element(2));
ll.push_back(new Element(3));
for(LinkedList::iterator it = ll.begin(); it != ll.end(); ++it){
cout << it->some_value << endl;
}
for(Element ele : ll) {
cout << ele.some_value << endl;
}
will print123123. What we don't understand is: Why are the ele in the range based for loop not pointers? Or better why does this not work:
for(Element * ele : ll) {
cout << ele->some_value << endl;
}
Basically we want to achieve the same with the custom linked list as can be achieved with the std vector:
vector<Element*> vec{new Element(1),new Element(2),new Element(3)};
for(Element * ele : vec)
{
cout<<ele->some_value<<endl;
}
LinkedList::begin()andLinkedList::end()return. Range based for loops will dereference those iterators for you. Note that eacheleis a copy of the elements inll. Use a reference to avoid that copy. To support your suggestion, you would need to be able to convertElement&toElement *implicitly.Element eleis not a pointer?for (const auto& ele : ...like one usually does...for(Element * ele : vec). I mean it is possible with the std vector, it should be possible with our custom linked list.