To initialize a filtered_graph object in construct

2019-08-29 04:52发布

问题:

I have a query in initializing filtered_graph in boost.

here is my code,

 // predicate 
 template <typename TGraph>
 struct edge_predicate
 {
   bool operator()(const typename boost::graph_traits<TGraph>::edge_descriptor& v) const
   {
     return true //testing purpose
    }
  };

// class 
class A{

 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Node_Info, Edge_Info > Graph_t;
 typedef boost::filtered_graph < Graph_t , edge_predicate > FilteredGraphType_t;

 // members
 Graph G;
 FilteredGraphType_t FG() ; // empty filter graph ???

 // constructor for A
 template < ..typename list > 
 A (  ... input iterators list...  )
 {
    fill the graph G with some values passed in constructor argument list.

    // predicate
    edge_predicate< Graph_t >  EPred;

    //filtered_graph
    FilteredGraphType_t FG( G, EPred);   // I am passing on edge predicate.
 }

};

// member function:
FilteredGraphType_t&   get_filtered_graph() 
{   
   return FG; 
 }

Problem:

FG is a member of class A, and G is also a member of A. Initially G is empty object, so is FG I suppose,..

In constructor of A, I fill Graph G with my logic ( such as number of vertices, edges etc ). Now, I want to create a filter FG (which is member of class A) over graph G (after G is filled).

The reason I want this is that this filtered graph is passed as a reference to some other class constructor. This forces me to make FG as member of class A. ( I could create new instance of filter graph in constructor of A itself, but it wont serve the purpose of returning the reference to FG. )

I hope it is clear. please suggest

回答1:

You can make your code compile and work OK. Try reorganizing the constructor as follows:

// constructor for A
 template < ..typename list > 
 A (  ... input iterators list...  )
      : G() //calls constructor for G
      , EPred(...) //some constructor for predicate
      , FG( boost::make_filtered_graph(G, EPred) ) //creates your filtered graph
{
// fill the graph G with some values passed in constructor argument list.
}

The rest of code can remain unchanged.