views:

214

answers:

4

Hello,

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

+5  A: 

Bad idea to derive from std::list (it is not designed to be derived from).

Use a member variable of type std::list.

class myContainer
{
  std::list<myObject>   m_data;
  public:

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

  public:

    myContainer::const_iterator begin() const
    {
      return m_data.begin();
    }

    myContainer::const_iterator end() const 
    {
      return m_data.end();
    }

  private:
    myContainer::iterator begin()
    {
      return m_data.begin();
    }

    myContainer::iterator end()
    {
      return m_data.end();
    }
};
Martin York
Have updated question to reflect this good suggestion, although the main issue still remains. Seems only solution is to either rename the private ones or static_cast to const before calling begin()/end().
WilliamKF
+3  A: 

You need to change the name of the private begin end. The compiler can't differentiate by only the return type

This works for me : note the _begin _end names

#include <list>


class myObject {};

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

public:
  myContainer::const_iterator begin() const {
    return std::list<myObject>::begin();
  }
  myContainer::const_iterator end() const {
    return std::list<myObject>::end();
  }
private:
  myContainer::iterator _begin() {
    return std::list<myObject>::begin();
  }
  myContainer::iterator _end() {
    return std::list<myObject>::end();
  }
};

void myFunction(myContainer &container) {
  myContainer::const_iterator aItr = container.begin();
  myContainer::const_iterator aEndItr = container.end();
  for (; aItr != aEndItr; ++aItr) {
    const myObject &item = *aItr;
    // Do something const on container's contents.
  }
}

int main(){
    myContainer m;
    myFunction(m);
}
fabrizioM
Although renaming will do the trick here, you keep having trouble if you want to throw your object into a generic function expecting `begin()` and `end()`...
xtofl
Seems like this is the only viable alternative other than doing static_cast<const myContainer>(container).begin()/end() for each caller.
WilliamKF
I you're going to do this, you should put the underscore at the end, like this: `begin_()` `end_()`. Names with leading underscores are reserved for the compiler and standard library implementations. See http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier
Emile Cormier
@Emilie Cormier I didn't know that. Thanks!. In python is a common idiom to name the private members with a leading _.@xtofl, if those functions are private, nobody but the class itself can use it, what generic function are you talking about? an example?
fabrizioM
+1  A: 

You might want to change the signature of your method Myfunction to this:

void myFunction(const myContainer &container) 

because a const method would be called on a const object only. What currently is happening is that you are trying to call a non-const method which in your case is private.

hype
But in some cases that would not suffice, have updated testcase to reflect this.
WilliamKF
+3  A: 

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.

UncleBens
Arg, posted mere seconds before me!
Potatoswatter
See http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier
Emile Cormier
@Emile: AFAIK, using a single underscore followed by a lower-case character is OK. Personally I wouldn't use this style indeed.
UncleBens