I am currently working on creating a header file that acts like the standard library. I am in the progress of implemeing .end() for my linked list. In my cpp file I have an integer pointer which is expected to recieve the value of the .end() like
int* itrend;
itrend = ca.end();
ca is just an instance of my object in my header file.
In my hpp file, I have a list iterator going to my listofvalues.end(). I would like to return the .end() value like
int* end()
{
std::list<value_type>::iterator i = listOfValues.end();
return (&*i);
}
My “List of values” is just a list holding several ints. eg
[1,2,3,4,5,6,7,8,9]
In my hpp file, I have a list iterator going to my listofvalues.end(). I would like to return the .end() value (which returns a list iterator); now this posess a problem that it returns the list iterator when I want to have an integer pointer. I have tried to deference it and take the address by using &* and this will give an error saying “List Iterator Not Dereferencable”. I know it’s because I’m trying to get the element after the last value, but that is exactly what I need to return, since I am trying to re-implement the linked list .end() method.
Does anyone know how I can have an integer pointer point to the List.end()?
Any assistance is appreciated.
To implement
begin()andend()in a style similar to STL returniterators, not pointers: