c++ - Destructor query -
I have this program, where I am passing a vector in the context of the function I am not releasing the object with the new object, because now ignore the memory leak due to this. After Output is: I am creating 5 objects so that The vector creates a copy around the object, but only your Add To see if he balances the count. myFunc and Inside this function, I am adding some element to the vector.
MyFunc () execution is complete I know how many times the constructor and the district was called.
before 5 7
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
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.
MyClass (const MyClass & amp; rhs) i (Rhs.i) {++ ctor; }
Comments
Post a Comment