copy constructor

Posted on 2003-12-09
Last Modified: 2010-04-17
under what all senario's copy constructor is invoked.hurry up guys
Question by:chikucoder
LVL 24

Expert Comment

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
LVL 24

Expert Comment

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 ) ;

Author Comment

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

Accepted Solution

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


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

Expert Comment

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

Expert Comment

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.

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 56
File.WriteAllLines problem at random C# ASP.NET 6 53
topping3 challenge 14 52
C# Error - Add Failed 12 53
A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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