| | |
Why is destructor called twice?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
C++ Syntax (Toggle Plain Text)
#include <vector> #include <iostream> using namespace std; class Test { public: Test() { cout<<"In TEST constructor"<<endl; } ~Test() { cout<<"In TEST destructor"<<endl; } }; int main() { Test t1; vector<Test> vec; vec.push_back( t1 ); cout<<"About to clear vector"<<endl; vec.clear(); }
I suspect I'm missing something fundamental. When I run this, I get the cout for the destructor twice. (Why would clearing the vector cause the destructor of Test to be called?)
./Temp
In TEST constructor
About to clear vector
In TEST destructor
In TEST destructor
A guess: The constructor code is the construction of t1. Then a copy constructor is used when it is pushed back on the vector. When the clear() is called, it calls the destructor for the object in the vector. Then t1's destructor is called when it goes out of scope.
#include <vector>
#include <iostream>
using namespace std;
class Test
{
public:
Test()
{
cout<<"In TEST constructor"<<endl;
}
~Test()
{
cout<<"In TEST destructor"<<endl;
}
};
int main()
{
Test t1;
vector<Test> vec;
vec.push_back( t1 );
cout<<"About to clear vector"<<endl;
vec.clear();
cout << "done\n";
}
/* my output
In TEST constructor
About to clear vector
In TEST destructor
done
In TEST destructor
*/ "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Makes sense. To test, I made it a vector of Test * and only got one destruction call:
./Temp
In TEST constructor
About to clear vector
done
In TEST destructor
----------------
I would imagine then that if my classes are large that I would be much better served (performance wise) to be holding pointers to my objects than copies of my objects....
C++ Syntax (Toggle Plain Text)
#include <vector> #include <iostream> using namespace std; class Test { public: Test() { cout<<"In TEST constructor"<<endl; } ~Test() { cout<<"In TEST destructor"<<endl; } }; int main() { Test t1; vector<Test *> vec; vec.push_back( &t1 ); cout<<"About to clear vector"<<endl; vec.clear(); cout<<"done"<<endl; }
In TEST constructor
About to clear vector
done
In TEST destructor
----------------
I would imagine then that if my classes are large that I would be much better served (performance wise) to be holding pointers to my objects than copies of my objects....
I can't help but wonder about your intent with pushing an address. Let's say you were reading records from a file into a temporary line of text. You could avoid the constructor and pass only a pointer to the temporary text, but you wouldn't be keeping unique copies of the data that you'd read. In the end you'd have a pile of pointers all pointing to something that is no longer there.
Be careful with what you are doing, and try to avoid premature optimization. Perhaps you could provide a little more of the 'big picture'; but in the end be careful.
Be careful with what you are doing, and try to avoid premature optimization. Perhaps you could provide a little more of the 'big picture'; but in the end be careful.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- multithreading & destructor call. (C++)
- FILE * cleanup in destructor. (C++)
- syntex Error (C)
- template issues - need expert debugger! (C++)
- C++ quiz .. test your knowlegde (and help me in the process!) (C++)
- How to be Crash Free (C++)
- dubble delete (C++)
Other Threads in the C++ Forum
- Previous Thread: Concordance, Linked Lists and classes
- Next Thread: Anyway can help me to solve this problem???
Views: 4123 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






