开发者

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 ListNo开发者_运维知识库de;
   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;
}


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.


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


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.


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 ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜