Consider these two classes (List<Type>
and List<Type>::iterator
)
// List<Type>
template<typename Type>
class List {
public:
class iterator;
iterator& iter();
private:
Type *elems; // Array
};
// List<type>::iterator
template<typename Type>
class List<Type>::iterator {
public:
Type *current;
Type operator* ();
private:
iterator (Type *current) : current(current) {}
};
template<typename Type>
typename List<Type>::iterator& List<Type>::iter () {
return (iterator(this->elems));
}
// Program
int main () {
List<int> *list = new List<int>();
List<int>::iterator iter = list->iter(); // When does iter get destroyed?
delete list;
}
I read about this return with parentheses return (...);
as a return-by-reference so to speak. I'm not sure, but I think iter
is created on the stack (because of the absence of new
). So my question is: When does iter
go out of scope? After iter()
, after main()
or somewhere else? Do I have to delete
it manually?