-2

I'm currently going through C++ Primer 5th Ed. and today I've reached the chapter about classes that deal with dynamic memory. Given, the following concept:

class StrVec{

  public:

    StrVec() : // the allocator member is default initialized
      elements(nullptr),
      first_free(nullptr),
      cap(nullptr) {}

    StrVec(const StrVec&);

    StrVec(std::initializer_list<std::string>);

    StrVec& operator=(const StrVec&);

    ~StrVec();
    std::string*        begin()     const       { return elements; }
    std::string*        end()       const       { return first_free; }
    //other functions

  private:

    std::allocator<std::string> alloc; 

    std::string *elements; 

    std::string *first_free; 

    std::string *cap;

    //other functions

};

And a StrVec object StrVec foo={"stack","overflow"}, how does a range-for loop work exactly (for(auto& el : foo) std::cout<<el<<std::endl). What's the "thing" that I'm iterating through?

JohhnieWH
  • 3
  • 2
  • 4
    Are you asking about dynamic memory allocation, or about [range for loops](http://en.cppreference.com/w/cpp/language/range-for)? – Some programmer dude Sep 05 '16 at 14:56
  • 3
    You've omitted the key member functions required for the range for loop... What are you actually asking? – LogicStuff Sep 05 '16 at 14:59
  • 2
    Something about an answer to this question. – Sam Varshavchik Sep 05 '16 at 15:00
  • @LogicStuff The implementation is not what I'm asking about. The loop works just fine. Take it like this, if it was the vector I would've been iterating through, it goes ---> v[0],v[1],v[2].. etc, but what am I iterating through in a StrVec object ? You can't use the subscript, obviously, nor do I have a container in the class – JohhnieWH Sep 05 '16 at 15:01
  • 1
    What is it, then? – LogicStuff Sep 05 '16 at 15:03
  • 2
    @JohhnieWH Without at least the declarations of a `begin()` and `end()` there's no way of telling what a [range-based for loop](http://en.cppreference.com/w/cpp/language/range-for) over `StrVec` would do (except cause compilation errors). – Biffen Sep 05 '16 at 15:04
  • @Biffen Oh, so the loop is using these ?` std::string* begin() const { return elements; } std::string* end() const { return first_free; }` // sorry for poor editing but it's the first time I use this site – JohhnieWH Sep 05 '16 at 15:06
  • 1
    **Dupe: http://stackoverflow.com/questions/8164567/how-to-make-my-custom-type-to-work-with-range-based-for-loops** – LogicStuff Sep 05 '16 at 15:06
  • @JohhnieWH Where did those come from?! Can you see now why we're asking to see more code? – Biffen Sep 05 '16 at 15:07
  • @Biffen Sorry, I thought it would be enough, I put them in the original post too, they were included in `//other functions` – JohhnieWH Sep 05 '16 at 15:08
  • 1
    Your question's title is pretty terrible. Fix it please! – Lightness Races in Orbit Sep 05 '16 at 15:08
  • Nope, still doesn't describe the question. – Lightness Races in Orbit Sep 05 '16 at 15:30

1 Answers1

1

What's the "thing" that I'm iterating through?

The range that is delimited by the result of foo.begin() and foo.end().

Yes, these function names are "special" in that regard. Call it a standard-approved convention.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055