In the following code, the destructor is called twice, and the constructor is called only once:
enum TFoo { VAL1, VAL2 }; class CFoo { public: TFoo mf; CFoo() { cout<<"hi c'tor1\n"; //mf = f; } CFoo(TFoo f) { cout<<"hi c'tor2\n"; mf = f; } CFoo(TFoo &f) { cout<<"hi c'tor3\n"; mf = f; } ~CFoo() { cout<<"bye\n"; } }; int main() { vector<CFoo> v; //v.assign(1, VAL1); v.push_back(VAL1); }
Code Outputs:
hi c'tor2 bye bye
I found a similar question that mentioned copy constructors, so I added them, but with the same result. Uncommenting the line //v.assign(1, VAL1); also does not change anything.
c ++ destructor
qtreez
source share