﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-守望-文章分类-C++</title><link>http://www.cppblog.com/gaowentao/category/6492.html</link><description>Let's start!</description><language>zh-cn</language><lastBuildDate>Wed, 04 Jun 2008 19:11:52 GMT</lastBuildDate><pubDate>Wed, 04 Jun 2008 19:11:52 GMT</pubDate><ttl>60</ttl><item><title>Shallow and Deep Copy</title><link>http://www.cppblog.com/gaowentao/articles/45468.html</link><dc:creator>winter</dc:creator><author>winter</author><pubDate>Wed, 26 Mar 2008 14:40:00 GMT</pubDate><guid>http://www.cppblog.com/gaowentao/articles/45468.html</guid><wfw:comment>http://www.cppblog.com/gaowentao/comments/45468.html</wfw:comment><comments>http://www.cppblog.com/gaowentao/articles/45468.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/gaowentao/comments/commentRss/45468.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/gaowentao/services/trackbacks/45468.html</trackback:ping><description><![CDATA[<font face=Arial size=2>Copy constructor is</font>
<ul>
    <li><font face="Arial, Helvetica, sans-serif" size=2>a constructor function with the same name as the class</font>
    <li><font face="Arial, Helvetica, sans-serif" size=2>used to make deep copy of objects.</font> </li>
</ul>
<p><font face="Arial, Helvetica, sans-serif" size=2>If a copy constructor is not defined in a class, the compiler itself defines one. This will ensure a shallow copy. If the class does not have pointer variables with dynamically allocated memory, then one need not worry about defining a copy constructor. It can be left to the compiler's discretion.</font></p>
<br>
<p><font face="Arial, Helvetica, sans-serif" size=2>But if the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor.<br></font></p>
<font face=Arial size=2><br>For ex:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class A &nbsp;&nbsp;//Without copy constructor<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int x;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A() {A = 10;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~A() {}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br></font><br><font face=Arial size=2>class B&nbsp;&nbsp;&nbsp; //With copy constructor<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; B()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = new char[20];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~B()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete name[];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Copy constructor<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; B(const B &amp;b)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = new char[20];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcpy(name, b.name);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</font><br><br>
<p><font face="Arial, Helvetica, sans-serif" size=2>Let us Imagine if you don't have a copy constructor for the class B. At the first place, if an object is created from some existing object, we cannot be sure that the memory is allocated. Also, if the memory is deleted in destructor, the delete operator might be called twice for the same memory location.</font></p>
<p><font face="Arial, Helvetica, sans-serif" size=2>This is a major risk. One happy thing is, if the class is not so complex this will come to the fore during development itself. But if the class is very complicated, then these kind of errors will be difficult to track.</font></p>
<br>
<img src ="http://www.cppblog.com/gaowentao/aggbug/45468.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/gaowentao/" target="_blank">winter</a> 2008-03-26 22:40 <a href="http://www.cppblog.com/gaowentao/articles/45468.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>