|
-
December 13th, 2006, 02:45 AM
#1
Copy Constructor ????
Hi...
wat is Copy Constructor ??? when it is invoked ???
Regards,
Sundar.G
-
December 13th, 2006, 03:24 AM
#2
Re: Copy Constructor ????
Basically it is to create a deep copy of an object.
A copy constructor takes the class name.
Its called when you create an object from another. Or when you assign an object to another.
Code:
// Assume these are implemented elsewhere, for simplicity sake
class Foo
{
public:
Foo(); // Regular
Foo(Foo &f); // Copy
...
};
Foo f1, f2;
Foo f3(f1); // Copy Constructor
f1.DoSomething();
f2 = f1; // Copy Constructor
Most compilers will provide one if you do not create it yourself, a very basic one though.
-
December 13th, 2006, 05:11 AM
#3
Re: Copy Constructor ????
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
December 13th, 2006, 12:59 PM
#4
Re: Copy Constructor ????
Actually the assignment operator is called when making assignments to existing objects. It is a common cause of confusion.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 13th, 2006, 02:48 PM
#5
Re: Copy Constructor ????
 Originally Posted by TheCPUWizard
Actually the assignment operator is called when making assignments to existing objects. It is a common cause of confusion. 
To confuse even more (and hopefully clarify by doing so):
Code:
A a0;
A a1(a0); // copy constructor
A a2 = a1; // copy constructor
a2 = a1; // assignment operator
Jeff
-
December 13th, 2006, 02:49 PM
#6
Re: Copy Constructor ????
Hence the reason for me using the word "existing" in my post
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 13th, 2006, 03:03 PM
#7
Re: Copy Constructor ????
 Originally Posted by TheCPUWizard
Hence the reason for me using the word "existing" in my post 
And also the reason I used the word "clarify" in my post 
Jeff
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|