Deriving an iterator
Hi there,
I've derived std::vector and std::vector::iterator
..
Unfortunately the derived iterator does not work as I would like.
///// test.cpp //////////////////////////////////////////////////
#include <iostream>
#include <vector>
using std::vector;
template<typename T>
struct Myvec : public vector<T> {
using vector<T>::vector;
class myiterator;
myiterator begin1() {return myiterator{static_cast<vector<T>>(*this).begin()}; }
myiterator end1() {return myiterator{static_cast<vector<T>>(*this).end()}; }
const myiterator begin1()const {return myiterator{static_cast<vector<T>>(*this).begin()}; }
const myiterator end1() const {return myiterator{static_cast<vector<T>>(*this).end()}; }
};
template<typename T>
struct Myvec<T>::myiterator : public vector<T>::iterator{
// CONSTRUCTOR
myiterator(const typename vector<T>::iterator& it) : vector<T>::iterator{it} {}
using vector<T>::iterator::iterator;
};
int main()
{
Myvec<int> vec = {1, 2, 3};
for (Myvec<int>::myiterator it = vec.begin1(); it != vec.end1(); ++it) {
std::cout << *it << '\n';
}
return 0;
}
///////////////////////////////////////////////////////
Compile (on linux) with
c++ -std=c++11 -o test test.cpp
EXPECTED ("wanted") OUTPUT:
1
2
3
The OUTPUT I unfortunately get:
0
0
3
What's wrong with the code?
How does one get it working so that the derived iterator is usable (in the way I want)
Thanks.