Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return a null iterator in c++?

Tags:

c++

std

I am writing a wrapper class which looks like this:

class Wrapper{
private:
    std::list<People> men;
    std::list<People> woman;

    /**
        some bizzar logics
    **/
public:
    std::list<People>::iterator getMeTheNextOne(){};
}

The problem is, sometime, I need to return an empty (or NULL) iterator, saying that there is no more 'suitable' people in either list any more. If I simply return men.end() or women.end(), is the user gonna catch this?

Imaging the user have following code:

Wrapper wo;
std::list<People>::iterator it = wo.getMeTheNextPeople();
if(it == /*what should I put here? i cannot access the list members of the Wrapper*/){
// do something here
}
like image 251
James Bond Avatar asked Sep 08 '25 18:09

James Bond


1 Answers

Returning a list iterator, when the user doesn't have access to the list which the iterator is coming from, is weird and ugly. Why not return a pointer to a People instead, which can be NULL?

like image 53
Sneftel Avatar answered Sep 10 '25 06:09

Sneftel