Making a deep copy

Hi all. I can't get a grip on how to make a deep copy when working with more then one class.

I have the two classes Transaktion and TransaktionsLista, where Transaktion creates a Transaction (date, type, payer, amount of money, amount of friends, name of friends that now ows that amount of money) and TransaktionsLista creates an Array with the Transactions.

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
class Transaktion
 {
  private:
   string datum;
   string typ;
   string namn;
   double belopp;
   int ant_kompisar;
   string *kompisar;
   void deallocate();

  public:
   Transaktion();
   ~Transaktion();
   Transaktion(const Transaktion & t );
   string haemta_namn();
   double haemta_belopp();
   int haemta_ant_kompisar();
   bool finnsKompis( string namnet );
   bool laesEnTrans( istream &is );
   void skrivEnTrans( ostream &os );
   string haemta_kompisar(int val);
 };

 class TransaktionsLista
{
 private:
   Transaktion *trans;
   int antalTrans;

 public:
   TransaktionsLista();
   ~TransaktionsLista();
   void laesin( istream & is );
   void skrivut( ostream & os );
   void laggTill( Transaktion & t );
   double totalkostnad();
   double liggerUteMed( string namnet );
   double aerSkyldig( string namnet );
   PersonLista FixaPersoner();
 };


When i add one transaction everything works fine. But when i am about to add my second transaction i need to make a deep copy, that i can't get a grip on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void TransaktionsLista::laggTill( Transaktion & t )
{
    Transaktion *p=0;
    p = new Transaktion[antalTrans+1];

      int i;
    for ( i = 0; i < antalTrans; i++ )
	{
	  p[i] = trans[i];
	}


delete [] trans;
trans = p;
antalTrans++;
trans[antalTrans-1]=t;
}


When i use p[i]=trans[i]; it doesn't seem to use my copyconstructor that i made (tested it with cout).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Transaktion::Transaktion( const Transaktion & t )  // kopieringskonstruktor
  : datum(t.datum), typ(t.typ), namn(t.namn), belopp(t.belopp), ant_kompisar(t.ant_kompisar)
{
    cout << "kopierar" << endl;
  if ( ant_kompisar > 0)
    {
      kompisar = new string[ant_kompisar]; 

      for ( int i = 0; i < ant_kompisar; i++ )
        kompisar[i] = t.kompisar[i]; 
    }
  else
    kompisar = 0;
}
Perhaps it'd be better to overload the = operator for the class and perform the deep copy in there.

http://www.cplusplus.com/doc/tutorial/classes2/
Since it is a school asignment i don't think my teacher will allow us to overload the = operator. One of the points of the assignment is to learn deep copy.

Thanks anyway.
well you are assigning one pointer to the other of the same type, you are not doing anything with the actual object, just the pointer that can point to the object. If you wanted to test your copy constructor you have to do something like this:
1
2
3
4
Transaktion t; // set your variables to make it distinguishable
Transaktion y = t; // this would call your copy constructor
// the only other time that a copy constructor is typically called is
// in a function when you are returning a object by value 

What a copy constructor does is when there isnt already an object of that type instantiated and there is a need to copy an existing object, that is when it is called. That is different from the assignment operator in which that does a deep copy to an already existing object of that type.
If you have any questions let me know.
Topic archived. No new replies allowed.