Boost logo

Boost Users :

Subject: Re: [Boost-users] [Graph] troubles with pointer properties
From: Nat Goodspeed (nat_at_[hidden])
Date: 2011-10-11 07:48:05


On Oct 10, 2011, at 10:00 PM, Paolo Fogliaroni <paolo_at_[hidden]> wrote:

> I'm experiencing some problems in using graph internal properties.

This isn't a Boost Graph issue, this is a C++ issue.

> typedef adjacency_list< vecS, vecS, directedS, property<vertex_p_t, generic_vertex_property*> > Graph;
...

> Indeed from such get function I have objects of type generic_vertex_property,
> thus, when trying to access a property of the son classes I got this compile error:
> error: ‘class generic_vertex_property’ has no member named ‘name’

Yes, the compiler cannot assume that any given instance of generic_vertex_property might also be an instance of a subclass.

If you can conceptualize the problem as an operation that you need to perform on every vertex, but implemented in two different ways depending on the vertex subclass type, you could give the base class and its subclasses a virtual method. Put your access logic into that method, so that code needing to access 'name' is implemented in the subclass that defines 'name'.

If you can't approach it that way, you might try dynamic_cast with pointers - but this isn't the preferred approach. (Imagine adding a third property subclass...)

Look up virtual methods and dynamic_cast.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net