Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why doesn't this list copy work?

Reply
Thread Tools

Why doesn't this list copy work?

 
 
Eric Lilja
Guest
Posts: n/a
 
      08-11-2005
Hello!
Consider the following complete program (with sample output). There's
something wrong with my call to std::copy() in the copy constructor of
the Unit class. It fails to copy the list. I could solve it by looping
manually through the argument's list and call push_back for each
element, but I'd like to know what's wrong with my std::copy() call.
Here's the code with sample output:
#include <algorithm> /* std::copy() */
#include <iostream>
#include <list>

class Unit
{
public:
Unit(int n)
{
m_list.push_back(n);
}

Unit(const Unit& u)
{
/* This call fails to copy the list. */
std::copy(u.m_list.begin(), u.m_list.end(), m_list.begin());
}

void add_to_list(int n)
{
m_list.push_back(n);
}

void print_list() const
{
std::list<int>::const_iterator itr = m_list.begin();

while(itr != m_list.end())
{
std::cout << *itr << std::endl;
++itr;
}
}

private:
std::list<int> m_list;
};

int
main()
{
Unit u1(4711);

u1.add_to_list(123);
u1.add_to_list(456);

Unit u2(u1);

std::cout << "Printing u1:" << std::endl;
u1.print_list();

std::cout << "Printing u2:" << std::endl;
u2.print_list();

return 0;
}


Sample output:
$ ./copyctor.exe
Printing u1:
4711
123
456
Printing u2:

Thanks for any replies!

/ E

 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      08-11-2005
Eric Lilja wrote:
> Hello!
> Consider the following complete program (with sample output). There's
> something wrong with my call to std::copy() in the copy constructor of
> the Unit class. It fails to copy the list. I could solve it by looping
> manually through the argument's list and call push_back for each
> element, but I'd like to know what's wrong with my std::copy() call.


You can't copy something if the destination does not have enough space.
In your case, the Unit being copy-constructed is empty. You *must* use
insert() or push_back() on the list.

You have two solutions: either a manual loop or an iterator adaptor.

> Here's the code with sample output:
> #include <algorithm> /* std::copy() */
> #include <iostream>
> #include <list>
>
> class Unit
> {
> public:
> Unit(int n)
> {
> m_list.push_back(n);
> }
>
> Unit(const Unit& u)
> {
> /* This call fails to copy the list. */
> std::copy(u.m_list.begin(), u.m_list.end(), m_list.begin());


for (std::list<int>::iterator itor=u.m_list.begin();
itor!=u.m_list.end(); ++itor)
{
u.push_back(*itor);
}

or

std::copy(u.m_list.begin(), u.m_list.end(),
std::back_inserter(m_list));

> }
>
> void add_to_list(int n)
> {
> m_list.push_back(n);
> }
>
> void print_list() const
> {
> std::list<int>::const_iterator itr = m_list.begin();
>
> while(itr != m_list.end())
> {
> std::cout << *itr << std::endl;
> ++itr;
> }
> }
>
> private:
> std::list<int> m_list;
> };
>
> int
> main()
> {
> Unit u1(4711);
>
> u1.add_to_list(123);
> u1.add_to_list(456);
>
> Unit u2(u1);
>
> std::cout << "Printing u1:" << std::endl;
> u1.print_list();
>
> std::cout << "Printing u2:" << std::endl;
> u2.print_list();
>
> return 0;
> }
>
>
> Sample output:
> $ ./copyctor.exe
> Printing u1:
> 4711
> 123
> 456
> Printing u2:



Jonathan

 
Reply With Quote
 
 
 
 
n2xssvv g02gfr12930
Guest
Posts: n/a
 
      08-11-2005
Eric Lilja wrote:
> Hello!
> Consider the following complete program (with sample output). There's
> something wrong with my call to std::copy() in the copy constructor of
> the Unit class. It fails to copy the list. I could solve it by looping
> manually through the argument's list and call push_back for each
> element, but I'd like to know what's wrong with my std::copy() call.
> Here's the code with sample output:
> #include <algorithm> /* std::copy() */
> #include <iostream>
> #include <list>
>
> class Unit
> {
> public:
> Unit(int n)
> {
> m_list.push_back(n);
> }
>
> Unit(const Unit& u)
> {
> /* This call fails to copy the list. */
> std::copy(u.m_list.begin(), u.m_list.end(), m_list.begin());


Use the following instead. Well implemented classes like STL will
implement safe copy constructors.

m_list = u.m_list;


> }
>
> void add_to_list(int n)
> {
> m_list.push_back(n);
> }
>
> void print_list() const
> {
> std::list<int>::const_iterator itr = m_list.begin();
>
> while(itr != m_list.end())
> {
> std::cout << *itr << std::endl;
> ++itr;
> }
> }
>
> private:
> std::list<int> m_list;
> };
>
> int
> main()
> {
> Unit u1(4711);
>
> u1.add_to_list(123);
> u1.add_to_list(456);
>
> Unit u2(u1);
>
> std::cout << "Printing u1:" << std::endl;
> u1.print_list();
>
> std::cout << "Printing u2:" << std::endl;
> u2.print_list();
>
> return 0;
> }
>
>
> Sample output:
> $ ./copyctor.exe
> Printing u1:
> 4711
> 123
> 456
> Printing u2:
>
> Thanks for any replies!
>
> / E
>


vector u2 is not updated by std:copy() hence it will still appear empty!
The copy did work and is dangerous in this case as you need to be sure
enough memory is availble to copy to!
To copy the contents of a vector to another just use an assignment as
outlined below.

std::vector<int> a,b;
..
..
// initiase a with some values
..
..
b = a;

John B
 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      08-11-2005

"n2xssvv g02gfr12930" <> wrote in message
news:XgIKe.2428$...
> Eric Lilja wrote:
>> Hello!
>> Consider the following complete program (with sample output). There's
>> something wrong with my call to std::copy() in the copy constructor of
>> the Unit class. It fails to copy the list. I could solve it by looping
>> manually through the argument's list and call push_back for each
>> element, but I'd like to know what's wrong with my std::copy() call.
>> Here's the code with sample output:
>> #include <algorithm> /* std::copy() */
>> #include <iostream>
>> #include <list>
>>
>> class Unit
>> {
>> public:
>> Unit(int n)
>> {
>> m_list.push_back(n);
>> }
>>
>> Unit(const Unit& u)
>> {
>> /* This call fails to copy the list. */
>> std::copy(u.m_list.begin(), u.m_list.end(), m_list.begin());

>
> Use the following instead. Well implemented classes like STL will
> implement safe copy constructors.
>
> m_list = u.m_list;
>


Better yet:

Unit(const Unit& u):m_list(u.m_list){}

and your initializer list should contain all of your class' members in the
order they are declared in your clas declaration.

Jeff


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
Copy constructor: why can't I copy objects as if they were structs? rdc02271 C++ 24 12-27-2005 12:38 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57