Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: delete object or delete members?
    primarykey
    data
    text
    <p>I would like to ask about the functional difference; maybe ask for an example scenario, where I should choose from one of the options in the main method below:</p> <pre><code>#include &lt;iostream&gt; using namespace std; class A{ private: int x, y; public: A(int, int); }; class B{ private: int *x, *y; public: B(int, int); ~B(); }; A:: A(int x, int y){ this-&gt;x = x; this-&gt;y = y; } B:: B(int x, int y){ this-&gt;x = new int(x); this-&gt;y = new int(y); } B:: ~B(){ delete this-&gt;x; delete this-&gt;y; } int main(){ int x = 0, y = 0; A* objA = new A(x, y); // line 1 B objB1(x, y); // line 2 B* objB2 = new B(x, y); // line 3 delete objA; delete objB2; return 0; } </code></pre> <p>I understand that the second declaration in the main method <code>B objB1(x, y)</code> is obviously different from the other 2, but can someone please explain the functional difference between the constructors in lines labelled 1 and 3? Is there any bad practice in either of the declarations?</p> <p>Thanks</p> <p>NAX</p> <p><strong>UPDATE</strong></p> <p>First of all, I appreciate all of the answers that everyone is giving, I am really getting some good insight. I have edited the code above as a few of the answers pointed out that I haven't deleted the objects that I used, which is fair and all, but that was not the purpose of my question. I just wanted to gain some insight on the functional difference between the different approaches to creating the classes. And thanks to all that targeted that point. I am reading through the answers still.</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.
 

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