Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6178835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:31:59+00:00 2026-05-24T00:31:59+00:00

class X { public: typedef std::list<int> Container; // (1) const Container& GetElements() const; //

  • 0
class X {
  public:
    typedef std::list<int> Container;

    // (1)
    const Container& GetElements() const;

    // (2)
    Container::iterator ElementBegin();
    Container::iterator ElementEnd();

    // (3)
    CustomIterator GetElementIterator();

  private:
    Container m_container;
};

I’m looking for a consistent and clean way of providing iterators to encapsulated containers to the caller. I came up with the three ideas marked in the source code above.

  1. Provides size(), begin() and end(), all perfect for read access. However, because the returned Container reference is const, you’ll only be able to use const_iterator. Returning the reference non-const is bad, because the container itself could be modified (e.g. clear()).
  2. Provides non-const access to the elements, however we’d often need an own size() method (like GetElementCount()). iterator::distance() could be used, but that may be inefficient for some containers (where operator++/-- is called repeatedly to calculate the distance).
  3. Provides a custom iterator with methods like next() etc. Still an own size() method is required.

I highly bet there’re nicer solutions, so if you know any, I’d be glad to see them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:32:00+00:00Added an answer on May 24, 2026 at 12:32 am

    A mix of (2) and (3) would probably be what I’d do :

    class X {
      public :
        typedef std::list<int> ElementContainer;
        typedef ElementContainer::size_type ElementSizeType;
        typedef ElementContainer::iterator ElementIterator;
        typedef ElementContainer::const_iterator ConstElementIterator;
    
        ElementIterator elementBegin() { return m_container.begin(); }
        ElementIterator elementEnd() { return m_container.end(); }
    
        ConstElementIterator elementBegin() const { return m_container.begin(); }
        ConstElementIterator elementEnd() const { return m_container.end(); }
    
        ElementSizeType elementSize() const { return m_container.size(); }
    
      private :
        ElementContainer m_container;
    };
    

    It still leaves room to write custom iterators (by changing the typedefs), but as long as the ones provided by the container are ok, they can be used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this simplified example: #include <list> typedef std::list<int> IntList; class KindaIntList { public: IntList::const_iterator
Let's say I have a class: class NumberCollection { public: typedef std::set<int> SetType; typedef
This is my action class public Iterator<CgConsultant> getSearchresult() { List<CgConsultant> list =userSearch.getConsultantMatches(searchstring); System.out.println(The size
I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container
public ref class ScriptEditor : public Form { public: typedef map<UInt32, ScriptEditor^> AlMap; static
typedef void (FunctionSet::* Function)(); class MyFunctionSet : public FunctionSet { protected: void addFunctions() {
Assume the following class: public class MyEnum: IEnumerator { private List<SomeObject> _myList = new
Given the following class public class Foo { public int FooId { get; set;
If I have the list, typedef std::list<MyClass *> listMyClass; How do I iterate through
I have a function getA() with the following signature: class A { public: typedef

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.