I created a class with some static data. Something like that:
class Object { public: Object(); Object( Point center, double area ); Object( int cx, int cy, double area ); ~Object();
than in it the constructor and destructor I did:
Object::Object() { this->setCenter( Point() ); this->setArea( 0.0 ); objCounter++; totalArea += 0; areaMean = totalArea / objCounter; } Object::~Object() {
So, I wanted to know how many objects were created, the total area and the middle area. I tested it with simple instructions like:
Object first; Object second; cout << Object::getObjCounter() << "\n";
And all is well. The feature class counts the number of rows correctly. I tested using simple arrays:
Object test[ 10 ]; cout << Object::getObjCounter() << "\n";
Surprisingly ... it works as it should; I tested dynamic allocation:
Object *test = new Object[ 10 ]; cout << Object::getObjCounter() << "\n"; delete [] test;
Again ... it works. But when I try:
vector< Object > test( 10, Object() ); cout << Object::getObjCounter() << "\n";
It gives me zero in stdout ... I put flags in the constructor and destructor to understand why this is happening. And this shows me that when I use the mapped vector operator, the constructor is only called on, than the destructor is called in the sequence !!!! What for???? Doesn't make sense to me! Can anyone explain this to me? Besides, can someone help me in using the vector to achieve the same effect that I have with simple arrays, which means: is there a bunch of objects inside something that correctly counts? The fact is that I need vector functions, such as deleting and adding elements, resizing, but I do not want to reinvent the wheel. Thanks in advance.