"Alf P. Steinbach" <alfps@start.no> wrote in message
* jgscott@gmail.com:
I've been trawling around for an answer to this question and thought
I'd try here. I have a class Graph, which has a std::list<Node> as a
class member. Node it itself a class that makes extensive use of
pointers to represent various forms of data in the nodes of graphs,
along with pointers to neighboring nodes. This necessitates a deep
copy that iterators over the list. My copy constructor looks like:
DecomposableGraph::DecomposableGraph(const DecomposableGraph& G)
{
// Some stuff
// And now the iterator declaration:
std::list<Node, const Node&, const Node*>::iterator jt_i, jt_l, jt_m;
// use these iterators to do deep copy of G
}
The GNU compiler on my G4 Mac laptop does not object to this
declaration, which I cobbled together by trial and error message. The
code compiles, and the copy constructor works exactly as intended.
The GNU compiler on a Linux machine that I want to port this code to,
however, objects, claiming I can only have 1 member of the list
template declaration rather than 3. This leads me to believe I've
stumbled upon a peculiarity of the Mac implementation of the STL which
makes my declaration interpretable, albeit nonstandard. The problem,
as I understand it, is that I can't use a normal list<Node>::iterator
to iterator over class members of the const G the way I could over a
normal non-const function argument. But I don't know how to modify
the declaration in general to make such iteration possible.
Can anyone clue me in to the "standard" way to solve this problem, or
correct my understanding? Thanks for your time.
If possible you should accept the default copy constructor for
DecomposableGraph, which then invokes the std::list copy constructor,
which invokes copy constructors for all Node instances in the list, and
here's where you need to deep copy so you should probably either provide a
custom copy constructor for Node or delegate even further, or, best of
all, see if use of e.g. shared_ptr can avoid the need for deep copying.
std::list has two template parameters: the node type T, and an allocator
type that defaults to std::allocator<T>.
Three actual parameters shouldn't compile.
Also, what is the actual declaration of your std::list in DecomposableGraph?