Code :
------------------------------------------------------
Results :Code:class passByValueSucks { public : passByValueSucks(){} passByValueSucks(const passByValueSucks& p) { cout << "copy constructor called o no" << endl ; } passByValueSucks& operator=(const passByValueSucks& p) { if (this == &p) return *this; cout << "operator = " << endl ; return *this; } }; int main(int argc, char* argv[]) { passByValueSucks d; cout << "test vector " << endl ; vector<passByValueSucks> v ; v.push_back(d); v.push_back(d); v.push_back(d); v.push_back(d); system("pause"); return 0; }
--------------------------------------------
Questions :Code:test vector copy constructor called o no copy constructor called o no copy constructor called o no copy constructor called o no copy constructor called o no copy constructor called o no copy constructor called o no copy constructor called o no copy constructor called o no copy constructor called o no
1) why does the vector call 10 times the copy constructor ?
i would expect at max 4 times.
2) it is an overkill that vector calls copy constructor, ( cost to call normal constructor + copy constructor). Any way to make vector not pass by value but by reference and call delete when all when it gets deleted ? or do i have to create my own vector class.




Reply With Quote