Safe way for getting/finding a vertex in a graph with custom properties -> good programming practice

Posted by Shadow on Stack Overflow See other posts from Stack Overflow or by Shadow
Published on 2010-03-17T12:45:34Z Indexed on 2010/04/17 18:33 UTC
Read the original article Hit count: 170

Filed under:
|
|

Hi,

I am writing a Graph-class using boost-graph-library. I use custom vertex and edge properties and a map to store/find the vertices/edges for a given property.
I'm satisfied with how it works, so far.
However, I have a small problem, where I'm not sure how to solve it "nicely". The class provides a method

Vertex getVertex(Vertexproperties v_prop)

and a method

bool hasVertex(Vertexproperties v_prop)

The question now is, would you judge this as good programming practice in C++?
My opinion is, that I have first to check if something is available before I can get it. So, before getting a vertex with a desired property, one has to check if hasVertex() would return true for those properties.
However, I would like to make getVertex() a bit more robust. ATM it will segfault when one would directly call getVertex() without prior checking if the graph has a corresponding vertex. A first idea was to return a NULL-pointer or a pointer that points past the last stored vertex. For the latter, I haven't found out how to do this.
But even with this "robust" version, one would have to check for correctness after getting a vertex or one would also run into a SegFault when dereferencing that vertex-pointer for example. Therefore I am wondering if it is "ok" to let getVertex() SegFault if one does not check for availability beforehand?

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost