
Hello everyone,
my first post here, so I hope not to miss some mailing list rule.
I'm experiencing some problems in using graph internal properties.
I have to generate a graph from a database and I need to differentiate nodes
in my graph,
namely I will have 2 different node categories, say cat1_node and cat2_node.
Each category should have its own property attached.
Ex: say N1 is a cat1_node and N2 is a cat2_node. I want N1 to have a "name"
(string)
property while N2 a "length" (int) property and both have a "label" (string)
property.
To achieve this I though I could use inheritance in the following manner:
//FATHER PROPERTY CLASS
class generic_vertex_property{
public:
generic_vertex_property(){type = 0;}
int type;
std::string label;
};
//SON PROPERTY CLASS
class cat1_property: public generic_vertex_property{
public:
std::string name;
};
//SON PROPERTY CLASS
class cat2_property: public generic_vertex_property{
public:
int length;
};
In this way, I though, I can define my graph something like:
typedef adjacency_list< vecS, vecS, directedS, property

On Oct 10, 2011, at 10:00 PM, Paolo Fogliaroni
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
> 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.
participants (2)
-
Nat Goodspeed
-
Paolo Fogliaroni