Search code examples
c++vectoriteratornamespacesinitialization

how to make an iterator class a member of a container class c++


I have an container class (myvector) and an iterator class (const_myiterator)

I want to initialize an iterator like this

auto myit = myvector<int>::const_myiterator{myvec.cbegin()};

As I can do with std::vector class

auto it = std::vector<int>::const_iterator{vec.cbegin()};

But I don't know how to implement this


Solution

  • I am very sorry for I asked this question without trying to solve the problem myself. Thanks all who said me about a nested classes. I heard about them before but never used yet and therefore didn't remember them. The answer was given in comments by @Some programmer dude "You define classes inside classes just like you define any class. Template or not doesn't matter. With that said, the iterator class doesn't have to be a template, if defined inside a template then it already have access to the surrounding templates arguments." Thank you very much!