c++ - Destructor query -


I have this program, where I am passing a vector in the context of the function myFunc and Inside this function, I am adding some element to the vector.

I am not releasing the object with the new object, because now ignore the memory leak due to this.

After MyFunc () execution is complete I know how many times the constructor and the district was called.

Output is:

  before 5 7   

I am creating 5 objects so that Ctor is 5 . But why is dtor 7 ? Where does the additional two counts? Am I missing something?

  #include #include using namespace std; Stable int CTOR = 0; Fixed int dtor = 0; Classes Myclass {Public: Myclass (Int N) {i = n; Ctor ++; // cout & lt; & Lt; "MyClass CTOR" & lt; & Lt; Ctor & lt; & Lt; Endl; } ~ Myclass () {dtor ++; // cout & lt; & Lt; "Mylcass DTOR" & lt; & Lt; Dtor & lt; & Lt; Endl; } Private: int i; }; Zero myFunc (vector & lt; MyClass & gt; & amp; m); Zero myFunc (Vector   

The vector creates a copy around the object, but only your int < / Code> Manufacturers' increments ctor . It is not accounted for copied objects, and because you have not provided one, the compiler has provided it for you.

Add

  MyClass (const MyClass & amp; rhs) i (Rhs.i) {++ ctor; }   

To see if he balances the count.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -