Solved

copy constructor

Posted on 2003-12-09
6
368 Views
Last Modified: 2010-04-17
under what all senario's copy constructor is invoked.hurry up guys
0
Comment
Question by:chikucoder
[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
6 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9910257
Copy constructor is invoked when 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 :

SomeClass A, B;

A.Assign(); // Assuming you have a method Assign which fills this.
B = A;        // Now the copy constructor is invoked
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9910264
Purpose of copy constructor is to make a new object based on an existing object, where both objects belong to the same class.

another example is
SomeClass B( A ) ;
0
 
LVL 1

Author Comment

by:chikucoder
ID: 9910270
how it is different from assignment operator
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 24

Accepted Solution

by:
shivsa earned 50 total points
ID: 9910361
in above example
B = A;  // invoke the assignment operator after invoking the default constructor.

while

SomeClass B( A ) ;  // invoke the copy constructor
queueClass y = x ;  // invoke the copy too.
-------------------------
0
 

Expert Comment

by:smallbug005
ID: 9910520
1. At the time of object creation
2.At datatype casting time
3. At = assignment operator invokation
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9910634
1) MyObject a(myObjectInst);
2) MyObject a = myObjectInst;
3) a = (MyObject)myObjectInst;       // if a has been initialised it will call the copy ctor then the assignment op...
                                                    // if not then it will call the copy ctor twice,
                                                         // once for the cast and once for the initialisation of a.
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 2 56
Why is enum singleton a better approach than static factory 3 66
How do I fix this situation in GIT? 2 75
Excel Web Add-in Where is Visual Basic used 9 80
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

734 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