473,785 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Nodea s 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:

DecomposableGra ph::Decomposabl eGraph(const DecomposableGra ph& G)
{
// Some stuff
// And now the iterator declaration:
std::list<Node, const Node&, const Node*>::iterato r 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>::ite rator
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 6629
"Alf P. Steinbach" <al***@start.no wrote in message
news:13******** *****@corp.supe rnews.com...
>* jg*****@gmail.c om:
>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<Nodea s 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:

DecomposableGr aph::Decomposab leGraph(const DecomposableGra ph& G)
{
// Some stuff
// And now the iterator declaration:
std::list<Node , const Node&, const Node*>::iterato r 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>::ite rator
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
DecomposableGra ph, 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 DecomposableGra ph?
Sep 25 '07 #2
jg*****@gmail.c om 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<Nodea s 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:

DecomposableGra ph::Decomposabl eGraph(const DecomposableGra ph& G)
{
// Some stuff
// And now the iterator declaration:
std::list<Node, const Node&, const Node*>::iterato r jt_i, jt_l, jt_m;
// use these iterators to do deep copy of G
Huh? std::list<usual ly 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>::ite rator
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_iterato r
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.no said:
>
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 DecomposableGra ph?
class DecomposableGra ph: public GGM
{
public:
// Lots of stuff

private:
//more stuff, and then
set<ushortNodeS et;
list< JunctionTreeNod e JunctionForest;
}

class JunctionTreeNod e
{
public:
//some stuff
// Here is where the deep copy is necessary...
list< list<JunctionTr eeNode>::iterat or 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<usual ly 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<JunctionTr eeNode>::iterat or
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
5808
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
7
1604
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 limited only to VC++. Listing 1 ================== #include <iostream> using namespace std;
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.