C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Edit Report
Why do I need a copy constructor? Posted by Saket Shukla on 29 Nov 2000 at 10:41 PM
Hi All,<br>
<br>
My basic question is what is the benefit I get by having a copy constructor? I mean from the optimization and efficiency point of view. Because ultimately insde the copy constructor you would be using the '=' assignment operator only so why not have only a overloaded '=' assignment operator instead of having a copy constructor.<br>
<br>
In essence, why do I need a copy constructor? Can't I do with only an overloaded '=' operator?


Edit Report
Re: Why do I need a copy constructor? Posted by Andrew Hanson on 30 Nov 2000 at 6:31 AM
: Hi All,<br>
: <br>
: My basic question is what is the benefit I get by having a copy constructor? I mean from the optimization and efficiency point of view. Because ultimately insde the copy constructor you would be using the '=' assignment operator only so why not have only a overloaded '=' assignment operator instead of having a copy constructor.<br>
: <br>
: In essence, why do I need a copy constructor? Can't I do with only an overloaded '=' operator?<br>
: <br>
<br>
A copy constructor is only used when you declare a class object and initialize it with an existing object of the same class. Therefore, they come into play if you plan on implementing a class constructor of the following form:<br>
<br>
MyClass::MyClass( MyClass Copy );<br>
<br>
Such as if I do this:<br>
<br>
CString MyString1( "Hello World!" );<br>
CString MyString2( MyString1 );<br>
<br>
In the above case, a copy constructor is called. The compiler should automatically provide both default copy constructors and assignment operators. For many people, the defaults will be sufficient, however there is trouble when dealing with classes that contain dynamically allocated members. Consider this example of a class:<br>
<br>
class MyClass<br>
{<br>
private:<br>
&nbsp;&nbsp;&nbsp;&nbsp;char *pMessage;<br>
public:<br>
&nbsp;&nbsp;&nbsp;&nbsp;MyClass( char * );<br>
&nbsp;&nbsp;&nbsp;&nbsp;~MyClass();<br>
};<br>
<br>
Suppose the constructor dynamically allocates space for the argument, copies the argument's data into this space and assigns the resulting pointer to the pMessage member variable. Then if I do this:<br>
<br>
MyClass Class1( "My name is uncle bob." );<br>
<br>
the pMessage pointer gets the address of a dynamically allocated space holding the contents "My name is uncle bob." Now suppose I do this:<br>
<br>
MyClass Class2( Class1 );<br>
<br>
And I want the effect of this to be that Class2's<br>
data is copied from Class1's data. If I have not explicitly defined a copy constructor, then the provided default does a simple copy of all the data elements which will result in Class2's pMessage pointer pointing to the exact same space as Class1's pMessage pointer. Now, if Class1 goes out of scope and the destructor is called, what happens to Class2? Since it was pointing to a data area also owned by Class1, and since Class1 has been destroyed, Class2 probably has garbage in it. In this case, you must explicitly define a copy constructor such as this one:<br>
<br>
MyClass::MyClass( const MyClass& Copy )<br>
{<br>
int Length = strlen(Copy->pMessage) + 1;<br>
this->pMessage = new char[Length];<br>
memcpy( this->pMessage, Copy->pMessage, Length );<br>
}<br>
<br>
With this defined, Class2 would have gotten a completely new pointer pointing to a new memory area that holds the same character data as Class1's pointer. Then, when Class1 is destroyed, Class2 isn't affected since the memory locations pointed to by the pointers are not shared.<br>
<br>
Bottomline, if your class does not have any dynamically allocated elements, then the default copy constructor provided by the compiler will do just fine. If not, you must explicitly define one yourself if you plan on declaring class instances like I did up above with the 'CString' classes. Also, if you don't plan on using your classes where you would need to call a copy constructor, then of course you wouldn't need one. Kind of a long explanation, but there wasn't any other way I could think of at the moment to describe this.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.