Solved

Help with copy constructor

Posted on 1998-10-10
10
297 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 191
Embarcadero C++ Builder XE2 TDateTime 8 61
is twain_32.dll cmpatible with windows 10 ? 10 124
Best book to learn C++ 4 70
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

919 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

12 Experts available now in Live!

Get 1:1 Help Now