Solved

Help with copy constructor

Posted on 1998-10-10
10
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Better understanding on C++ Class serialization and formats 9 86
Dialogbox API leak? 18 109
Issues with C++ Class 19 109
Android development question 2 81
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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