What overloaded member function called in if(iterator)
-
Hi, Can anyone tell me which overloaded function is called, below in Line 2: vector::iterator it = nArray.begin(); // Line 1 if(it) //What overloaded function called here // Line 2 .................. As I know in this case overloaded (operator void *) function is called, But I d'nt find any such overloaded function in STL iterator class. Regards Devendra Chandola
-
Hi, Can anyone tell me which overloaded function is called, below in Line 2: vector::iterator it = nArray.begin(); // Line 1 if(it) //What overloaded function called here // Line 2 .................. As I know in this case overloaded (operator void *) function is called, But I d'nt find any such overloaded function in STL iterator class. Regards Devendra Chandola
As you point out, there's no official procedure in the STL specification to test an iterator in such a way. What's happening is that, for
vector
alone, and for your particular compiler,vector::iterator
happens to be an actual pointer, so the expression makes sense --it tests whether the pointer is non-null, which makes little sense in the given context. This is non-portable and you are not guaranteed that the test yield any significative result. Most likely what's intended is the following:if(it!=nArray.end()){
...
}Hope this helps, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!
-
As you point out, there's no official procedure in the STL specification to test an iterator in such a way. What's happening is that, for
vector
alone, and for your particular compiler,vector::iterator
happens to be an actual pointer, so the expression makes sense --it tests whether the pointer is non-null, which makes little sense in the given context. This is non-portable and you are not guaranteed that the test yield any significative result. Most likely what's intended is the following:if(it!=nArray.end()){
...
}Hope this helps, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!
-
Hi Joaquín, Thanks for suggestion I still want to know which overloaded function of iterator is called when we write: if(iterator) Regards Devendra Chandola
There is no general answer to your question. Actually, you should not even ask the question because the "if (iterator)" should not generally compile. However, in your case, the magic of typedef's probably makes it valid and equivalent to "if (pointer)". Also, the "suggestion" to do "if ( iterator == container.end() )" is NOT a suggestion: it is the only valid way to check for iterator validity. my 2 cents... Guy Prémont
-
Hi Joaquín, Thanks for suggestion I still want to know which overloaded function of iterator is called when we write: if(iterator) Regards Devendra Chandola
DevendraC wrote: I still want to know which overloaded function of iterator is called when we write: if(iterator) Set a breakpoint on that if statement, and step into the line (F11) and you'll see. --Mike-- LINKS~! Ericahist updated! | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD