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

  • Home
  • SEARCH
  • 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 670221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:14:19+00:00 2026-05-14T00:14:19+00:00

I have a class that includes a std::list and wish to provide public begin()

  • 0

I have a class that includes a std::list and wish to provide public begin() and end() for const_iterator and private begin() and end() for just plain iterator.

However, the compiler is seeing the private version and complaining that it is private instead of using the public const version.

I understand that C++ will not overload on return type (in this case const_iterator and iterator) and thus it is choosing the non-const version since my object is not const.

Short of casting my object to const before calling begin() or not overloading the name begin is there a way to accomplish this?

I would think this is a known pattern that folks have solved before and would like to follow suit as to how this is typically solved.

class myObject {
public:
  void doSomethingConst() const;
};

class myContainer {
public:
  typedef std::list<myObject>::const_iterator const_iterator;
private:
  typedef std::list<myObject>::iterator iterator;

public:
  const_iterator begin() const { return _data.begin(); }
  const_iterator end()   const { return _data.end();   }
  void reorder();
private:
  iterator begin() { return _data.begin(); }
  iterator end()   { return _data.end();   }
private:
  std::list<myObject> _data;
};

void myFunction(myContainer &container) {
  myContainer::const_iterator itr = container.begin();
  myContainer::const_iterator endItr = container.end();
  for (; itr != endItr; ++itr) {
    const myObject &item = *itr;
    item.doSomethingConst();
  }
  container.reorder(); // Do something non-const on container itself.
}

The error from the compiler is something like this:

../../src/example.h:447: error: `std::_List_iterator<myObject> myContainer::begin()' is private
caller.cpp:2393: error: within this context
../../src/example.h:450: error: `std::_List_iterator<myObject> myContainer::end()' is private
caller.cpp:2394: error: within this context

Thanks.

-William

  • 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-14T00:14:19+00:00Added an answer on May 14, 2026 at 12:14 am

    I think your only option is to rename the private methods (if you need them in the first place).

    In addition I believe you should rename the typedefs:

    class MyContainer
    {
    public:
         typedef std::list<Object>::const_iterator iterator;
         typedef iterator const_iterator;
    
         const_iterator begin() const;
         const_iterator end() const;
    
    private:
         typedef std::list<Object>::iterator _iterator;
         _iterator _begin();
         _iterator _end();
         ...
    };
    

    Containers are supposed to typedef both iterator and const_iterator. A generic function accepting a non-const instance of your container might expect to make use of the iterator typedef – even if it is not going to modify the elements. (For example BOOST_FOREACH.)

    It will be fine as far as const correctness goes, because should the generic function actually try to modify the objects, the real iterator type (being a const_iterator) wouldn’t let it.

    As a test, the following should compile with your container:

    int main()
    {
        myContainer m;
        BOOST_FOREACH(const myObject& o, m) 
        {}
    }
    

    Note that m is not const, but we are only trying to obtain const references to the contained types, so this should be allowed.

    • 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
I have a State class that includes configuration as well as some state-specific methods.
I have a class includes.vb that holds some variables (sharing them with other pages)
My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
I have class that looks like this: class A { public: class variables_map vm
I have a class with a static std::map member variable that maps char s
error C2071: 'Lexicon::list' : illegal storage class I have a class that reads a
I have a class Class which has a member std::list, I want to search
Class: Class: private: ... vector<string> words; vector< list<int> > vints; public: myFunction(...) I am
I have a DAL class library that is included in my program as a

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.