views:

92

answers:

4
/** @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;
}
+1  A: 

The question is, if such syntax is so easy then why make a copy method at all :> (unless you're one of those defensive people who want's copies explicitly stated -- then I submit, I'm one of them too).

You may also be interested in doing a copy (assignment) operator instead:

List& List::operator=(const List& aList)
{
    //
}

As for the inability to call the copy constructor see the C++ FAQ Lite on Constructors. This thread also asks the same question.

The inability to invoke constructors explicitly from class is a part of the C++ Standard document, but maaan, you don't want to read that thing... yet ;-)

Kornel Kisielewicz
Care to go into further detail why you can't call a copy constructor explicitly?
Brandon
My assignment is to make a copy method but since I already have a copy constructor (cc) I figured I would just have that method call the cc instead.
Brandon
Actually, I need to do a deep copy. Your solution only provides a shallow copy.
Brandon
Brandon, no. It calls your COPY CONSTRUCTOR, just as you wanted :>. If a copy constructor is provided, on a `List a = b;` the COPY CONSTRUCTOR will be called, even if an ASSIGNMENT OPERATOR is specified.
Kornel Kisielewicz
The assignment operator only points the head of the new list to the head of the other list, thus a shallow copy is made. There is only one list. I verified that the new list holds the exact same address in memory as the other list. My copy constructor creates an entirely separate copy of the old list.
Brandon
@Kornel: The assignment operator is different from the copy constructor. If you want to call both, you need `*this = List(aList)`. If you want to avoid `operator=`, you need `this->~List(); new(this) List();` — but that's terrible style.
Potatoswatter
@Kornel - `(*this) = aList;` does NOT invoke the copy constructor, it invokes operator=.
R Samuel Klatchko
Kornel Kisielewicz
+2  A: 

In your driver program, you have

List bList;
bList.copy(aList);

Instead, invoke the copy constructor with either

List bList(aList);

or

List bList = aList;

…Looking at your "copy" method: A constructor creates a new instance. Your List::copy method calls the copy constructor, creating a new instance of List on the stack. Then it returns, and your new instance is gone.

What you probably want instead of a "copy" method is to define an assignment operator,

List& List::operator=(const List& aList)
{
   if (this != &aList)
   {
      // Do your copying here
   }

   return *this;
}

Then your driver could say

List bList;
// ...Presumably manipulate bList in some other ways in-between...
bList = aList;

To invoke the assignment operator from inside another method of the same class, say

*this = aList;

or

operator=(aList);

I find the latter awkward. But referring to an operator explicitly by name can be necessary if you want to get a pointer to the member function.

Jon Reid
+4  A: 

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.

R Samuel Klatchko
I don't think the tweak is a recommendation. It is a nice trick (for showing at programming parties) but makes the code harder to read. The standard techniques (the first one above) is still the recomended way of doing it. In the long run neither is faster than the other, but hiding the copy operation just makes people do a double take before they see the implied copy into the parameter.
Martin York
Martin - I can't remember exactly where I read about this recommendation, but it can definitely be faster when the function is called with an rvalue. The compiler can then invoke the function without doing an extra copy. When the the function is called with an lvalue, the performance is the same.
R Samuel Klatchko
+1  A: 

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&).

Potatoswatter