Boost logo

Boost Users :

Subject: [Boost-users] [BGL / General] member of struct is a reference - why?
From: Max Limper (max.limper_at_[hidden])
Date: 2012-01-10 14:35:37

Hi everybody,
I was just going through the "File Dependency" example for the graph
library and found this piece of code which is a very simple cycle

struct cycle_detector : public dfs_visitor<>
    cycle_detector( bool& has_cycle)
      : _has_cycle(has_cycle) { }

    template <class Edge, class Graph>
    void back_edge(Edge, Graph&) {
      _has_cycle = true;
    bool& _has_cycle;

It takes a reference and manipulates the externally declared variable
hasCyle via that reference. I tried to change hasCylce to a "regular"
member variable, and I just want to read it afterwards, like

  cycle_detector detector();

  boost::depth_first_search(graph, boost::visitor(detector));

  if (detector.foundCycles)
      //do something

I also thought about giving the cycle detector other member variables,
like a list of back edges that gets filled. However, I found that none
of those variables gets set the right way, starting with the
very simple "bool hasCycle" member.

I guess there must be a reason why the person that made the example
chose to use a reference, and it only works that way... any ideas why?

Thanks a lot in advance.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at