5 Replies - 563 Views - Last Post: 10 February 2010 - 10:02 AM Rate Topic: -----

#1 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Deep Copy and = Operator

Posted 08 February 2010 - 10:28 PM

I am trying to figure out how to properly code a deep copy constructor and = operator for my class but I am having a lot of trouble with it actually working. Any pointers or help would be appreciated. :D


#include "aircraft.h"
#include "people.h"


class Ship {
public:
  Ship ();
  Ship (std::string name);
  ~Ship(){ }
  Ship(const Persons& copy)
  {
    persons = copy.persons;

  }

private:
  People* persons;
  std::string name;
  int persons_max;
  int persons_count;
};




Is This A Good Question/Topic? 0
  • +

Replies To: Deep Copy and = Operator

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,132
  • Joined: 07-September 06

Re: Deep Copy and = Operator

Posted 08 February 2010 - 10:37 PM

Assuming that your People class has a copy constructor you could do something like so:
persons = new People(copy.persons);


Then that should go through and be in charge of copying each person from the copy People to the new one.

Without seeing the People class I can't say whether or not it will work out without some additional editing, but that is the general idea.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Deep Copy and = Operator

Posted 09 February 2010 - 03:01 AM

View Postgordonheimer, on 09 February 2010 - 05:28 AM, said:

I am trying to figure out how to properly code a deep copy constructor and = operator for my class but I am having a lot of trouble with it actually working. Any pointers or help would be appreciated. :D


#include "aircraft.h"
#include "people.h"


class Ship {
public:
  Ship ();
  Ship (std::string name);
  ~Ship(){ }
  Ship(const Persons& copy)
  {
    persons = copy.persons;

  }

private:
  People* persons;
  std::string name;
  int persons_max;
  int persons_count;
};
Assuming 'persons' is intended to be used as a dynamically allocated array, it would be easier to use a 'C++ array' (called a vector), then the whole problem will sort itself out automatically since vectors/C++ arrays will automatically copying their stored data when copied
#include <vector>

class Ship {
public:
  Ship ();
  Ship (std::string name);

private:
  std::vector<People> persons;
  std::string name;
};



To create a copy constructor, you need to be careful, since you're allocating memory 'by hand' (something which you should avoid doing); to be completely safe, you'll need to write a destructor and a copy-assignment operator too

class foo {};
class bar
{
    foo* arr;
    size_t size;
public:
    bar& operator=(bar const&);
};

#include <algorithm>
bar& bar::operator =(bar const& rhs)
{
    size = rhs.size;

    /* 'new' might fail, so use a temporary pointer
     *  so that the object maintains a good state in
     *  the event of something unexpected
     */
    foo* temp = new foo[rhs.size];

    std::copy(rhs.arr, 
              rhs.arr + rhs.size,
              temp);
    delete [] arr;
    arr = temp;
    return *this;
} 
Writing the copy-assignment operator first can help, since you can use this within the copy constructor (this is safe since performing delete on a null pointer has no effect)

bar::bar(bar const& rhs) : arr(0)
{
    *this = rhs;
} 

This post has been edited by Bench: 09 February 2010 - 03:20 AM

Was This Post Helpful? 1
  • +
  • -

#4 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Deep Copy and = Operator

Posted 09 February 2010 - 10:04 PM

I wrote a destructor and a deep copy constructor but I am not sure if I did the copy right. I tried to run the program in Unix and i got a core dump error.

~Ship(){delete [] persons;
            };
   Ship(const Ship& per):persons(NULL),persons_max(0),persons_count(0)
  { *this=per;} 



I thought for the deep copy I need to write a for loop and loop through the array? I am not sure
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Deep Copy and = Operator

Posted 10 February 2010 - 01:59 AM

you can use a for loop if you prefer; std::copy will do the same thing. What does your copy-assignment operator look like?
Was This Post Helpful? 0
  • +
  • -

#6 gordonheimer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 24-April 09

Re: Deep Copy and = Operator

Posted 10 February 2010 - 10:02 AM

I have never used the copy function but here is my operator.

const Ship& operator= (const Ship& s)
  {
   name=s.name;
   persons=s.persons;
   persons_max=s.persons_max;
   persons_count=s.persons_count;
   return *this 
  }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1