Solved

Help with copy constructor

Posted on 1998-10-10
10
299 Views
Last Modified: 2012-05-04
Hi everyone,
      
      First off I believe I have a pretty fair understanding of what a copy constructor is suppose to do, how it does it and why it does it. What I seem to be having trouble with is that Im not sure how to invoke the copy contructor. Which I know is possible. I simply would like to use the copy that is make within the copy constructor in the same class in a sorting algorithm.
      I have know problem creating another list, but if I can use the copy in the copy constructor... why bother?
      I posted below a copy constructor I have written. If some one could give me an example as to how to invoke it I would appreciate it

Thanks for any help,
bbarnette


queueClass::queueClass(const queueClass& Q)
{
      if(Q.Rear == NULL)
            Rear = NULL;

      else
      {
            Rear = new queueNode;
            Rear->Item = Q.Rear->Item;

            queueNode* Nu = Rear;

      for (Rear = Q.Rear; Rear != NULL; Rear = Rear->Next)
      {
            Nu->Next = new queueNode;
            Nu = Nu->Next;
            Nu->Item = Rear->Item;
      }
      Nu->Next = NULL;
      }
}  // end copy constructor
0
Comment
Question by:bbarnette
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 3

Expert Comment

by:vmano
ID: 1174781
To invoke a copy constructor you have to assign an instance of the class to another instance of the class. For example in your case,  a sample of the code would be :

queueClass x, y;

x.Assign(); // Assuming you have a method Assign which fills this queue
y = x; // Now the copy constructor is invoked


0
 

Author Comment

by:bbarnette
ID: 1174782
are you saying that any time that there is an instance of the class (in this case queueClass) that the copy constructor is invoked?
0
 

Author Comment

by:bbarnette
ID: 1174783
and as long as there are two instances assigned to the class and the second points to the first... thats all there is to it?

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 8

Expert Comment

by:Answers2000
ID: 1174784
You can also do this

queueClass x ;
// <--- fill in X here

// now invoke copy ctor to make y
queueClass y( x ) ;

In the example y=x, the copy constructor is invoked because you haven't created an operator= for queueClass, so the C++ compiler tries to figure out how to make y, given an x, and the copy constructor is the closest match.

Purpose of copy ctor is to make a new object based on an existing object, where both objects belong to the same class.

I don't understand your 2 part question on 2:56/3:03...

0
 
LVL 5

Expert Comment

by:yonat
ID: 1174785
Just a correction: The copy constructor will NOT be invoked if you write y=x. It will only be invoked if you write
    queueClass y(x);
Or if you pass a queueClass object as a by value argument to a function or if you return it by value from a function.

If you write y=x, the ASSIGNMENT OPERATOR will be called. If you haven't defined it, then the default assignment operator will be used, which is member-wise assignment.

To summerise: vmano's answer is not correct in this case, while Answers2000's comment is correct about how to invoke the copy constructor.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174786
Hold it.  Yonat is not completely correct either.

The copy constructor is invoked when you define an object and give it an initial value with the "=" sign.  (This is not the = operator.)   Thus buth

queueClass y( x ) ;

and

queueClass y = x ;

invoke the copy constructur.  The second one appears like it might invoke the assignment operator, but it does not.  On the other hand

queueClass y;
y = x ;

does invoke the assignment operator (after invoking the default constructor.)
0
 
LVL 3

Expert Comment

by:xyu
ID: 1174787
Hello... I;m back again:)
Nietod, You are right (as usually) !
0
 
LVL 5

Expert Comment

by:yonat
ID: 1174788
I stand corrected.
0
 

Author Comment

by:bbarnette
ID: 1174789
Nietod, if you wish to post as an answer I will award the points to you.

And Thanks Again!
0
 
LVL 22

Accepted Solution

by:
nietod earned 20 total points
ID: 1174790
he copy constructor is invoked when you define an object and give it an initial value with the "=" sign.  (This is not the = operator.)   Thus both

      queueClass y( x ) ;

      and

      queueClass y = x ;

invoke the copy constructur.  The second one appears like it might invoke the assignment operator, but it does not.  On the other hand

      queueClass y;
      y = x ;

does invoke the assignment operator (after invoking the default constructor.)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question