Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>~</code> signs that it is a destructor and when ever the object goes out of scope, corresponding destructor is called.</p> <p><strong>When the destructor is called ?</strong> </p> <p>Taking an example -</p> <pre><code>#include &lt;iostream&gt; class foo { public: void checkDestructorCall() ; ~foo(); }; void foo::checkDestructorCall() { foo objOne; // objOne goes out of scope because of being a locally created object upon return of checkDestructorCall } foo:: ~foo() { std::cout &lt;&lt; "Destructor called \t" &lt;&lt; this &lt;&lt; "\n"; } int main() { foo obj; // obj goes of scope upon return of main obj.checkDestructorCall(); return 0; } </code></pre> <p>Results on my system: </p> <pre><code>Destructor called 0xbfec7942 Destructor called 0xbfec7943 </code></pre> <p>This example just serves to indicate when a destructor is called. But destructor is written only when the class manages resources.</p> <p><strong>When class manages resources?</strong></p> <pre><code>#include &lt;iostream&gt; class foo { int *ptr; public: foo() ; // Constructor ~foo() ; }; foo:: foo() { ptr = new int ; // ptr is managing resources. // This assignment can be modified to take place in initializer lists too. } foo:: ~foo() { std::cout &lt;&lt; "Destructor called \t" &lt;&lt; this &lt;&lt; "\n"; delete ptr ; // Returning back the resources acquired from the free store. // If this isn't done, program gives memory leaks. } int main() { foo *obj = new foo; // obj is pointing to resources acquired from free store. Or the object it is pointing to lies on heap. So, we need to explicitly call delete on the pointer object. delete obj ; // Calls the ~foo return 0; } </code></pre> <p>Results on my system:</p> <pre><code>Destructor called 0x9b68008 </code></pre> <p>And in the program, you posted I don't see a need to write destructor. Hope it helps !</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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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