Re: Reference, but object unchanged?
* James Kanze:
Unless you're a real expert, I think you should start by making
your nodes (Knoten) uncopiable, and go from there. That will
force you to think in terms of identity (objects), and will
prevent any accidental copying. That will also mean that you
need to use vector<Knoten*>, rather than simply vector<Knoten>,
and if the vector is "owner" of the nodes, you'll have to run
through it deleting all of the elements before allowing
destruction of the vector.
It may be an explicit representation of nodes is not needed.
Anyway, the OP would probably benefit from using the Boost Graph library.
More generally, that means that you will have to consider memory
management issues. The simplest solution in your case is
probably to just install the Boehm collector, and use it.
That adds the complication of non-deterministic destruction (and zombie
objects, watered down class invariants etc. that programmers typically
introduce to deal with it) to the complications of the C++ deterministic
model, and it doesn't help with maintaining the graph structure on
removal of nodes. When you already have code that maintains the
structure correctly, doing correct C++ deterministic destruction is
almost trivial. So the Boehm collector doesn't really solve any problem
here, it just introduces new ones, both at the tool usage level and at
the programming level.
Instead, if automatic garbage collection is needed, i.e. if C++ is too
complicated for the combination of this development team / developer and
the problem at hand, then using a higher level language such as Java or
C#, or even a functional language, is the practical way to go.
That means dealing only with the problems of one resource handling
paradigm instead of dealing with the problems of many paradigms fighting
it out plus one of the most complicated programming languages in
Barring that, if any given node will only be used in a single
graph (no sharing), you can have the nodes belonging to the
Graph object: the only way to create a node in that case should
be through a factory function in the Graph object; that function
new's the node *and* adds it to a list of all nodes
(vector< Knoten* >). The destructor of Graph then delete's all
of the nodes in the list.
Yes, that's one way to do it from scratch. If nodes are ever removed,
however, back pointers will have to be introduced in order to do that
efficiently. As mentioned, it's not just about reclaiming memory but
also about maintaining the graph, and solving the latter (which is
necessary anyway) has a high chance of also solving the former.
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
Q: What is the most annoying thing on usenet and in e-mail?