Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your <code>operator+</code> receives copy of the argument passed to it.</p> <pre><code> CRectangle CRectangle::operator+ (CRectangle param) { CRectangle temp; temp.width = new int; temp.height = new int; *temp.width = *width + *param.width; *temp.height = *height + *param.height; return (temp); } </code></pre> <p>In the code:</p> <pre><code>CRectangle a(3,4),b(5,6),c; c=a+b; </code></pre> <p>More specifically, in the <code>a + b</code> copy of <code>b</code> is passed to <code>a.operator+</code>. At the end of the function this copy is destroyed. </p> <p>So, to sum up:</p> <ul> <li><p>Your statement <code>c = a + b;</code> can be separated in two parts:</p> <pre><code>temp = a + b; c = temp; </code></pre></li> <li><p>The first statement is basically this:</p> <pre><code>temp = a.operator+(b); </code></pre></li> </ul> <p>Now, your <code>operator+</code> is receiving <code>param</code> by value. This means that copy of object <code>b</code> will be created and you will have two identical objects: <code>b</code> and <code>param</code>. When this function returns, <code>param</code> object will be destroyed.</p> <p>Now, everything would be good <strong>except</strong>:</p> <ul> <li><p>Even <code>b</code> and <code>param</code> are separated objects, their pointers <code>width</code> and <code>height</code> points to a single memory location. Destructor of <code>param</code> will free this memory and <code>b</code>'s pointers will point to deallocated memory locations. In order to prevent this, you need to write copy-constructor:</p> <pre><code>CRectangle::CRectangle(const CRectangle&amp; param) { width = new int; // alocate separate memory locations! height = new int; *width = *param.width; *height = *param.height; } </code></pre></li> </ul> <p>To avoid copy-on-passing, pass by const-reference instead:</p> <pre><code>CRectangle CRectangle::operator+ (const CRectangle &amp;param) </code></pre> <p>As you noted, the same thing happens with <code>operator=</code>.</p> <p>Also, this code:</p> <pre><code>temp.width = new int; temp.height = new int; *temp.width = *width + *param.width; *temp.height = *height + *param.height; </code></pre> <p>can be rewritten as (also the types of <code>width</code> and <code>height</code> should be changed to <code>int</code>):</p> <pre><code>temp.width = width + param.width temp.height = height + param.height; </code></pre> <p>There's no need to have pointers as fields in your class, if they serve just like regular values.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload