[iterator_facade] Help with const iterator for binary file
Hi,
I have a class modeling a record-based read-only binary file and I have
written a random access iterator based on iterator_facade to iterate
over the records. The class looks like this:
class my_binary_file {
public:
typedef std::string key_type;
typedef unsigned int value_type;
typedef std::pair
In article
[...] Unfortunately, when I try to compile my project, I get the following error:
error: invalid conversion from 'const frequency_file* const' to 'frequency_file*'
Of course, the error message should read: error: invalid conversion from 'const my_binary_file* const' to 'my_binary_file*' Nicola
AMDG nicola wrote:
Hi, I have a class modeling a record-based read-only binary file and I have written a random access iterator based on iterator_facade to iterate over the records. The class looks like this:
class my_binary_file {
<snip>
explicit my_binary_file_iterator(my_binary_file* ff, unsigned int n) : ff_ptr_(ff), rec_num_(n) { }
my_binary_file* is *not* const here.
<snip>
};
public: typedef my_binary_file_iterator iterator;
iterator begin() *const* { return iterator(this, 0); }
this is a pointer to const. The constructor of iterator requires a pointer to non-const. In Christ, Steven Watanabe
In article <478810E0.7030407@providere-consulting.com>,
Steven Watanabe
AMDG
nicola wrote:
Hi, I have a class modeling a record-based read-only binary file and I have written a random access iterator based on iterator_facade to iterate over the records. The class looks like this:
class my_binary_file {
<snip>
explicit my_binary_file_iterator(my_binary_file* ff, unsigned int n) : ff_ptr_(ff), rec_num_(n) { }
my_binary_file* is *not* const here.
Thanks for pointing that out. In the tutorial example at http://www.boost.org/libs/iterator/doc/iterator_facade.html#tutorial-exam ple sect. "A constant node_iterator", shouldn't the constructor's parameter of const_node_iterator be const? That is, explicit const_node_iterator(const /* <== ? */ node_base* p) : m_node(p) {}
<snip>
};
public: typedef my_binary_file_iterator iterator;
iterator begin() *const* { return iterator(this, 0); }
this is a pointer to const. The constructor of iterator requires a pointer to non-const.
There is more than that, however: as you can see in my original post,
get_record() and seekg() are not const and that is a problem
because they are invoked by dereference(). I can compile successfully if
I const-qualify them, but this is not correct in my opinion.
The fact is, they do change the state of the object in a visible way.
That's the implementation of get_record() that I had omitted in the
original post (but that you could have easily guessed):
bool get_record(key_type& k, value_type& v) /* const ??? */ {
if (fs_.read(reinterpret_cast
AMDG nicola wrote:
There is more than that, however: as you can see in my original post, get_record() and seekg() are not const and that is a problem because they are invoked by dereference(). I can compile successfully if I const-qualify them, but this is not correct in my opinion. The fact is, they do change the state of the object in a visible way.
OK. Since begin and end return handles to the class that allow its state to change they should not be const. If they absolutely need to be const then you need to reset the the seek position after you use it. You can create private const versions get_record and seekg for this purpose. In Christ, Steven Watanabe
participants (2)
-
nicola
-
Steven Watanabe