LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2005, 08:28 PM   #1
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Rep: Reputation: 30
c++ copy() function


Here is my copy constructor:

Code:
  Unit(const Unit &u)
    {
      std::cout << "Unit copy constructor invoked\n";
      std::cout << "list size before " << u.members->size() << "\n";
      ownerVal = u.ownerVal;
      rank = u.rank;
      members = new list<UnitMember>();
      copy(u.members->begin(), u.members->end(), members->begin()); 
      std::cout << "list size after " << members->size() << "\n";
    }
Am I missing something? My list doesn't get copied and the "list size after" is 0.
 
Old 08-10-2005, 09:59 PM   #2
jayemef
Member
 
Registered: Aug 2005
Location: Juniata College, PA
Distribution: Ubuntu, Slackware
Posts: 67

Rep: Reputation: 15
Try:
Code:
std::copy(...)
 
Old 08-11-2005, 07:59 AM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Rewrite your call to copy as follows:
Code:
std::copy(u.m_list.begin(), u.m_list.end(), std::back_inserter(m_list));
I am not allocating the list object itself dynamically in this snippet, but it appears you are. Normally, that's not something you do not need to do. If you want to continue allocating the list object itself dynamically, replace . with ->
Here's a complete program that demonstrates how you should use std::copy.
Code:
#include <algorithm>
#include <iostream>
#include <list>

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

   Unit(const Unit& u)
   {
      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:
Code:
$ ./copyctor.exe 
Printing u1:
4711
123
456
Printing u2:
4711
123
456
 
Old 08-11-2005, 11:13 AM   #4
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
Thanks guys,
In my case this works:

Code:
std::copy(u.members->begin(), u.members->end(), std::back_inserter(*members));
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
postgres COPY function needed help vickr1z Linux - Newbie 1 11-08-2004 05:36 AM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration