Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you call a copy constructor from another method?

/** @file ListP.cpp
 *  ADT list - Pointer-based implementation. */

#include <iostream>
#include <cstddef>  // for NULL
#include <new>   // for bad_alloc
#include "ListP.h"  // header file

using namespace std;

List::List() : size(0), head(NULL)
{
} // end default constructor

List::List(const List& aList) : size(aList.size)
{
 if (aList.head == NULL)
  head = NULL; // original list is empty

 else
 { // copy first node
  head = new ListNode;
  head->item = aList.head->item;

  // copy rest of list
  ListNode *newPtr = head; // new pointer
  // newPtr points to last node in new list
  // origPtr points to nodes in original list
  for (ListNode *origPtr = aList.head->next; origPtr != NULL; origPtr = origPtr->next)
  { 
   newPtr->next = new ListNode;
   newPtr = newPtr->next;
   newPtr->item = origPtr->item;
  } // end for

  newPtr->next = NULL;
 } // end if
} // end copy constructor

void List::copy(const List& aList)
{
 List::List(aList);
} // end copy

I am trying to create a method called copy that simply calls the copy constructor. When I test this method in main the target list still remains empty. I have stepped through it and all the right lines are executed, but when the copy constructor returns nothing seems to be saved. I feel this has something to do with scope, but cannot pinpoint the problem. Here is the driver program:

#include <iostream>
using namespace std;

#include "ListP.h" 

int main ()
{
 List aList;

 ListItemType dataItem;
 aList.insert(1, 9);    
 aList.insert(2, 4); 
 aList.insert(3, 1); 
 aList.insert(4, 2); 

 List bList;
 bList.copy(aList);

 bList.retrieve(1, dataItem);
 cout << dataItem << endl;
 cout << bList.getLength() << endl;

 return 0;
}
like image 816
Brandon Tiqui Avatar asked Dec 10 '25 04:12

Brandon Tiqui


2 Answers

If I understand your question, you cannot do what you are trying to do.

Before you can call any other methods on an object, the object must be fully constructed (there is an exception here, I'll get back to that). Furthermore, an object can only be constructed once (*). Therefore, by the time you could call your copy method, the object would already be constructed and you can't (and shouldn't) construct it a second time.

The one exception to not being able to call a method on an object that is not fully constructed (i.e. the constructor has not yet returned) is that a constructor itself can call a method on the partially constructed object. So, you could call a copy method from the copy constructor, but not vice versa.

That said, if your object provides an optimized swap function, there is an standard trick that may be thinking of:

void List::copy(const List& aList)
{
    List acopy(aList);
    swap(*this, acopy);
}

This makes a copy of aList and then swaps the current contents of your object with this copy. acopy which now has the contents of your list before will be properly destructed when copy returns.

Finally, if you are going to do it, current recommendation is actually tweak it a bit and write it this way:

void List::copy(List aList)
{
    swap(*this, aList);
}

Under certain circumstances, this can be more efficient (and is never less efficient).

* - you can do weird things and construct an object twice with placement new. But there is no good reason to do that and many reasons why not to.

like image 145
R Samuel Klatchko Avatar answered Dec 12 '25 16:12

R Samuel Klatchko


Constructors are special because they are called only when the object is uninitialized. Therefore you can't call any as simple functions, copy or otherwise. C++ requires this because it helps write code which breaks less when you add features.

Probably what you want is to move the body of the copy constructor to Copy() and call Copy() from List::List(List const&).

like image 20
Potatoswatter Avatar answered Dec 12 '25 16:12

Potatoswatter