jeudi 22 juin 2017

how to use non-const function in a const function?

I have the following const function that uses the non-const function locate. locate can not be const because it might return a reference to *this but it never changes anything in this. I would like to be able to call locate so that I am not duplicating code, so what is the right way to use this non-const functon in this case?

As you can see, I even make the result of locate a const but that doesn't guarantee that I don't change something from within locate. So is there a way to make functions temporarily constant or something?

bool SegmentNode::find(const Segment& ss) const {
    const SegmentNode& parent = locate(ss);
    for (Segment in : parent.overlap_beg()) {
        if (ss == in)
            return true;
    }
    return false;
}

Aucun commentaire:

Enregistrer un commentaire