473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

copy constructors with iterators

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<Nodeas 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.

Sep 25 '07 #1
5 6575
"Alf P. Steinbach" <al***@start.nowrote in message
news:13*************@corp.supernews.com...
>* jg*****@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<Nodeas 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?
Sep 25 '07 #2
jg*****@gmail.com wrote:
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<Nodeas 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
Huh? std::list<usually takes two template parameters: the value type and a
user provided allocator (defaults to std::allocator< value_type >).
Implementations are allowed to add further parameters. Therefore, the above
might compile, however, it sure is weird to have a Node const & as an
allocator.
}

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.
std::list<Node>::const_iterator
Can anyone clue me in to the "standard" way to solve this problem, or
correct my understanding? Thanks for your time.

Best

Kai-Uwe Bux
Sep 25 '07 #3
On 2007-09-24 23:18:21 -0400, "Alf P. Steinbach" <al***@start.nosaid:
>
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.
Implementations are allowed to use three actual parameters, provided
any parameters in addition to the required ones come after them and
have default values. The three actual parameters in the original code
don't meet that requirement, but that's a much narrower statement.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 25 '07 #4
Punting the deep-copy responsibility down to a lower level seems like
a good idea, and I will try that... thank you!
Also, what is the actual declaration of your std::list in DecomposableGraph?
class DecomposableGraph: public GGM
{
public:
// Lots of stuff

private:
//more stuff, and then
set<ushortNodeSet;
list< JunctionTreeNode JunctionForest;
}

class JunctionTreeNode
{
public:
//some stuff
// Here is where the deep copy is necessary...
list< list<JunctionTreeNode>::iterator Neighbors;
//more stuff
};

So each node has data, and a list of pointers to other nodes (in a
list so iterators are stable) that say who the neighbors are. This is
probably not a great implementation, but I'm a statistician, not a
software guy, and this was the best way I could figure to do it for
the algorithms that happen over these graphical structures. Thanks
for your advice.

Sep 25 '07 #5
>
Huh? std::list<usually takes two template parameters: the value type and a
user provided allocator (defaults to std::allocator< value_type >).
Implementations are allowed to add further parameters. Therefore, the above
might compile, however, it sure is weird to have a Node const & as an
allocator.
I agree it looks strange, though I'm not au fait with the details of
the STL! I basically stumbled upon that declaration from the compiler
error message when I compiled using list<JunctionTreeNode>::iterator
as the declaration. After a little experimentation, the code then
compiled and works exactly as intended.

Sep 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
7
by: vj | last post by:
Hi! I recently came across this intresting behaviour shown by Visual C++ 6.0 compiler regarding Copy Constructors. Please tell me that is this the standard behaviour shown by all compilers or its...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.