Solved

Help with copy constructor

Posted on 1998-10-10
10
296 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
Hello... I;m back again:)
Nietod, You are right (as usually) !
0
 
LVL 5

Expert Comment

by:yonat
Comment Utility
I stand corrected.
0
 

Author Comment

by:bbarnette
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now