?
Solved

Help with copy constructor

Posted on 1998-10-10
10
Medium Priority
?
303 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
Independent Software Vendors: 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 80 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
Suggested Courses

771 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