<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: 9.11 &#8212; The copy constructor and overloading the assignment operator</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: saurabh</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-96906</link>
		<dc:creator>saurabh</dc:creator>
		<pubDate>Mon, 06 Feb 2012 00:23:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-96906</guid>
		<description>Here is answer to  why dont we use pass by pointer in copy constructor  so see following scenario
class ABC
{
public:
  ABC(){} /*Simple Constructor*/
  ABC(ABC * b) { }/*Copy Constructor*/
};
int main(int argc, char* argv[]){
  ABC * d1 = NULL; 
  ABC    d2 = d1; /*Calling Copy constructor*/
  return 0;
}
See above example , if we take pointer type object in copy constructor  
two flaws are here 
1) We can assign NULL value to simple object of that class
2) see this line  ABC    d2 = d1; 
here d2 is ABC type while d1 is ABC* type means as per rule we are violating basic rules by allowing to assign simple type with pointer type. 
If there is any pointer type member variable in side ABC class means DEEP Copy scenario like int * m_iVal; then it will crash out during calling copy constructor by passing NULL object, so stop such mistake at design time we do use const reference object of same class type in copy constructor as a parameter.Hoping this will clear you.</description>
		<content:encoded><![CDATA[<p>Here is answer to  why dont we use pass by pointer in copy constructor  so see following scenario<br />
class ABC<br />
{<br />
public:<br />
  ABC(){} /*Simple Constructor*/<br />
  ABC(ABC * b) { }/*Copy Constructor*/<br />
};<br />
int main(int argc, char* argv[]){<br />
  ABC * d1 = NULL;<br />
  ABC    d2 = d1; /*Calling Copy constructor*/<br />
  return 0;<br />
}<br />
See above example , if we take pointer type object in copy constructor<br />
two flaws are here<br />
1) We can assign NULL value to simple object of that class<br />
2) see this line  ABC    d2 = d1;<br />
here d2 is ABC type while d1 is ABC* type means as per rule we are violating basic rules by allowing to assign simple type with pointer type.<br />
If there is any pointer type member variable in side ABC class means DEEP Copy scenario like int * m_iVal; then it will crash out during calling copy constructor by passing NULL object, so stop such mistake at design time we do use const reference object of same class type in copy constructor as a parameter.Hoping this will clear you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: subbaiahtp</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-95982</link>
		<dc:creator>subbaiahtp</dc:creator>
		<pubDate>Tue, 06 Sep 2011 05:44:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-95982</guid>
		<description>Thanks subrat :-)</description>
		<content:encoded><![CDATA[<p>Thanks subrat :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: subrat</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-95941</link>
		<dc:creator>subrat</dc:creator>
		<pubDate>Sat, 27 Aug 2011 06:36:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-95941</guid>
		<description>class A {
private:
	char *fname;
    int age;
public:
	/*A() {
		std::cout&lt;&lt;&quot;zero ctr A\n&quot;&lt;fname = new char[strlen(fname)+1];
		this-&gt;age=age;
	  std::cout&lt;&lt;&quot;1 cstr---a....&quot;&lt;&lt;this&lt;&lt;std::endl;
	}
	A(const A&amp; val){
		age=val.age;
		fname = new char[strlen(val.fname)+1];
		strcpy(fname, val.fname);
		std::cout&lt;&lt;&quot;Cpy Cstr---A....&quot;&lt;&lt;this&lt;&lt;std::endl;
	}

	~A(){
		delete[] fname;
		std::cout&lt;&lt;&quot;Destr---A...&quot;&lt;&lt;this&lt;lname=new char[strlen(lname)+1];
		  this-&gt;marks= marks;
		  std::cout&lt;&lt;&quot;constrr B.......&quot;&lt;&lt;this&lt;&lt;std::endl;
	  }
	  B(const B&amp; val):aobj(val.aobj) {
		lname=new char[strlen(val.lname)+1];
		strcpy(lname, val.lname);
		marks= val.marks;
		std::cout&lt;&lt;&quot;copy Cstr---B::::&quot;&lt;&lt;this&lt;&lt;std::endl;

	}

	~B(){
		delete[] lname;
		std::cout&lt;&lt;&quot;Destr---B:::::&quot;&lt;&lt;this&lt;&lt;std::endl;
	}
};</description>
		<content:encoded><![CDATA[<p>class A {<br />
private:<br />
	char *fname;<br />
    int age;<br />
public:<br />
	/*A() {<br />
		std::cout&lt;&lt;&quot;zero ctr A\n&quot;&lt;fname = new char[strlen(fname)+1];<br />
		this-&gt;age=age;<br />
	  std::cout&lt;&lt;&quot;1 cstr&#8212;a&#8230;.&quot;&lt;&lt;this&lt;&lt;std::endl;<br />
	}<br />
	A(const A&amp; val){<br />
		age=val.age;<br />
		fname = new char[strlen(val.fname)+1];<br />
		strcpy(fname, val.fname);<br />
		std::cout&lt;&lt;&quot;Cpy Cstr&#8212;A&#8230;.&quot;&lt;&lt;this&lt;&lt;std::endl;<br />
	}</p>
<p>	~A(){<br />
		delete[] fname;<br />
		std::cout&lt;&lt;&quot;Destr&#8212;A&#8230;&quot;&lt;&lt;this&lt;lname=new char[strlen(lname)+1];<br />
		  this-&gt;marks= marks;<br />
		  std::cout&lt;&lt;&quot;constrr B&#8230;&#8230;.&quot;&lt;&lt;this&lt;&lt;std::endl;<br />
	  }<br />
	  B(const B&amp; val):aobj(val.aobj) {<br />
		lname=new char[strlen(val.lname)+1];<br />
		strcpy(lname, val.lname);<br />
		marks= val.marks;<br />
		std::cout&lt;&lt;&quot;copy Cstr&#8212;B::::&quot;&lt;&lt;this&lt;&lt;std::endl;</p>
<p>	}</p>
<p>	~B(){<br />
		delete[] lname;<br />
		std::cout&lt;&lt;&quot;Destr&#8212;B:::::&quot;&lt;&lt;this&lt;&lt;std::endl;<br />
	}<br />
};</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: subbaiahtp</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-95934</link>
		<dc:creator>subbaiahtp</dc:creator>
		<pubDate>Thu, 25 Aug 2011 10:40:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-95934</guid>
		<description>class A
{

private:

char *fname;
int age;

};

class B
{
private:
A aobj;
char *lname;
int marks;
};

Please let me know the B&#039;s copy constructor implementation?</description>
		<content:encoded><![CDATA[<p>class A<br />
{</p>
<p>private:</p>
<p>char *fname;<br />
int age;</p>
<p>};</p>
<p>class B<br />
{<br />
private:<br />
A aobj;<br />
char *lname;<br />
int marks;<br />
};</p>
<p>Please let me know the B&#8217;s copy constructor implementation?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mslade</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-95001</link>
		<dc:creator>mslade</dc:creator>
		<pubDate>Sat, 04 Dec 2010 15:41:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-95001</guid>
		<description>This confused me for a bit, too, so I&#039;ll offer my understanding that it might help someone else.

Returning *this will dereference the &quot;this&quot; pointer, and instead return the actual object itself.  What I got tripped up on is why it wasn&#039;t thus creating a copy of this and returning that.  The answer is because this function returns a Cents&amp; (reference).  As a result, the object is not copied and instead the capturing value gets the proper object.</description>
		<content:encoded><![CDATA[<p>This confused me for a bit, too, so I&#8217;ll offer my understanding that it might help someone else.</p>
<p>Returning *this will dereference the &#8220;this&#8221; pointer, and instead return the actual object itself.  What I got tripped up on is why it wasn&#8217;t thus creating a copy of this and returning that.  The answer is because this function returns a Cents&amp; (reference).  As a result, the object is not copied and instead the capturing value gets the proper object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sanjeev</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-90729</link>
		<dc:creator>Sanjeev</dc:creator>
		<pubDate>Mon, 23 Aug 2010 07:38:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-90729</guid>
		<description>Hi,
For the class A can I have copy constructor like A(const A*) instead of A(const A&amp;). What will be the difference. Can anybody please explain?</description>
		<content:encoded><![CDATA[<p>Hi,<br />
For the class A can I have copy constructor like A(const A*) instead of A(const A&amp;). What will be the difference. Can anybody please explain?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yuvrazbhatia21</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-90426</link>
		<dc:creator>yuvrazbhatia21</dc:creator>
		<pubDate>Tue, 17 Aug 2010 05:26:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-90426</guid>
		<description>welcom niit</description>
		<content:encoded><![CDATA[<p>welcom niit</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-73678</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Sat, 05 Dec 2009 12:57:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-73678</guid>
		<description>Hi, Alex,

  Actually, many compilers (e.g g++) avoid calling copy constructor when returning an object from function by so called NRV (named-return-value) optimization.

&lt;pre&gt;
class A {};
A f() 
{
  A a;
  return a;
}

int main()
{
  A aa = f();   // the normal constructor is called, no copy constructor
}
&lt;/pre&gt;

  However MS visual 2010 call the copy constructor when building debug version program, but won&#039;t call it when building release version. This is a disater for developper, who may see two results during development.</description>
		<content:encoded><![CDATA[<p>Hi, Alex,</p>
<p>  Actually, many compilers (e.g g++) avoid calling copy constructor when returning an object from function by so called NRV (named-return-value) optimization.</p>
<pre>
class A {};
A f()
{
  A a;
  return a;
}

int main()
{
  A aa = f();   // the normal constructor is called, no copy constructor
}
</pre>
<p>  However MS visual 2010 call the copy constructor when building debug version program, but won&#8217;t call it when building release version. This is a disater for developper, who may see two results during development.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Manas</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-71037</link>
		<dc:creator>Manas</dc:creator>
		<pubDate>Mon, 26 Oct 2009 05:52:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-71037</guid>
		<description>I want to take 1 pointer in my class which will point to some integer at run time. After that i will create one object and after that i will try to create one another object from the previous one with copy constructor. I was trying the following code but some how unable to access that or confuse in writing that code.
class Myclass
{
int * ptr ;
int i ;
Myclass()
{
ptr =new int ;
*ptr =20;
i=30;
}
Myclass (const Myclass &amp;a)
{
 this-&gt;i= a.i ;
 //But confuse in how to initialize the value which will be point by the pointer i am trying this way
 *p =*(a.p)//Please tell whether this is correct or not
}
};





void main()
{
Myclass M;
Myclass M1 =M ;
}</description>
		<content:encoded><![CDATA[<p>I want to take 1 pointer in my class which will point to some integer at run time. After that i will create one object and after that i will try to create one another object from the previous one with copy constructor. I was trying the following code but some how unable to access that or confuse in writing that code.<br />
class Myclass<br />
{<br />
int * ptr ;<br />
int i ;<br />
Myclass()<br />
{<br />
ptr =new int ;<br />
*ptr =20;<br />
i=30;<br />
}<br />
Myclass (const Myclass &amp;a)<br />
{<br />
 this-&gt;i= a.i ;<br />
 //But confuse in how to initialize the value which will be point by the pointer i am trying this way<br />
 *p =*(a.p)//Please tell whether this is correct or not<br />
}<br />
};</p>
<p>void main()<br />
{<br />
Myclass M;<br />
Myclass M1 =M ;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Li Yang</title>
		<link>http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/comment-page-1/#comment-67399</link>
		<dc:creator>Li Yang</dc:creator>
		<pubDate>Sat, 29 Aug 2009 09:43:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/#comment-67399</guid>
		<description>Hi Alex, Joris and Saravanan,

This site is really helpful!

I&#039;ve tried both versions and neither one work due to a typo of nmae. I corrected the typo and both ran without crash. Here is the details.

1. OS: Windows Vista Home.

2. Compiler: The compiler I used is Microsoft Visual C++ 2008 Express Edition. It&#039;s a free download trial version from Microsoft and will expire in 14 days. Any one who knows of a good free compiler and editor please recommend to me. Thank you in advance!

3. Outputs: I&#039;ve tried many times and the outputs appear to be stable. As Alex pointed out the output of the original version is garbage. Both statements &quot;delete [] s;&quot; and &quot;delete s;&quot; seem to produce the same garbage output from time to time. Joris&#039; version does display &quot;dd&quot; as expected because of the name=s.name; statement explicitly assigns &quot;dd&quot; to name.

4. Modifications: I modified the last statement from Joris&#039; version as follow
&lt;pre&gt;
cout &lt;&lt; &quot;s1.ini  = &quot; &lt;&lt; s1.ini  &lt;&lt; endl;
cout &lt;&lt; &quot;s1.name = &quot; &lt;&lt; s1.name &lt;&lt; endl;//prints successfully &quot;dd&quot;!! ..which i thought throw exception.
cout &lt;&lt; &quot;s1.age  = &quot; &lt;&lt; s1.age  &lt;&lt; endl;
&lt;!--formatted--&gt;&lt;/pre&gt;
The content displayed for s1.ini is always the symbol like a rotated &quot;8&quot;
The s1.name is always &quot;dd&quot; of course.
The s1.age is a random 7 digit integer eg 3141816 or 1568088.

Can anybody explain the results and the following questions?
The &quot;sar s1(*s);&quot; statement explicitly calls the copy constructor. Why is the assignment operator instead of the copy constructor actually executed? When will the copy constructor get called and when assignment operator? Can we force a specific function at our choice to run?

Thank you very much for your generous contributions!
Lee</description>
		<content:encoded><![CDATA[<p>Hi Alex, Joris and Saravanan,</p>
<p>This site is really helpful!</p>
<p>I&#8217;ve tried both versions and neither one work due to a typo of nmae. I corrected the typo and both ran without crash. Here is the details.</p>
<p>1. OS: Windows Vista Home.</p>
<p>2. Compiler: The compiler I used is Microsoft Visual C++ 2008 Express Edition. It&#8217;s a free download trial version from Microsoft and will expire in 14 days. Any one who knows of a good free compiler and editor please recommend to me. Thank you in advance!</p>
<p>3. Outputs: I&#8217;ve tried many times and the outputs appear to be stable. As Alex pointed out the output of the original version is garbage. Both statements &#8220;delete [] s;&#8221; and &#8220;delete s;&#8221; seem to produce the same garbage output from time to time. Joris&#8217; version does display &#8220;dd&#8221; as expected because of the name=s.name; statement explicitly assigns &#8220;dd&#8221; to name.</p>
<p>4. Modifications: I modified the last statement from Joris&#8217; version as follow</p>
<pre>
cout &lt;&lt; &quot;s1.ini  = &quot; &lt;&lt; s1.ini  &lt;&lt; endl;
cout &lt;&lt; &quot;s1.name = &quot; &lt;&lt; s1.name &lt;&lt; endl;//prints successfully &quot;dd&quot;!! ..which i thought throw exception.
cout &lt;&lt; &quot;s1.age  = &quot; &lt;&lt; s1.age  &lt;&lt; endl;
<!--formatted--></pre>
<p>The content displayed for s1.ini is always the symbol like a rotated &#8220;8&#8243;<br />
The s1.name is always &#8220;dd&#8221; of course.<br />
The s1.age is a random 7 digit integer eg 3141816 or 1568088.</p>
<p>Can anybody explain the results and the following questions?<br />
The &#8220;sar s1(*s);&#8221; statement explicitly calls the copy constructor. Why is the assignment operator instead of the copy constructor actually executed? When will the copy constructor get called and when assignment operator? Can we force a specific function at our choice to run?</p>
<p>Thank you very much for your generous contributions!<br />
Lee</p>
]]></content:encoded>
	</item>
</channel>
</rss>

