Vector does not create objects properly

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(); //and other public stuffs here... private: Point center; double area; static double totalArea; static int objCounter; static double areaMean; }; 

than in it the constructor and destructor I did:

 Object::Object() { this->setCenter( Point() ); this->setArea( 0.0 ); objCounter++; totalArea += 0; areaMean = totalArea / objCounter; } /*this is just the default constructor I have two others that increment real area, not 0*/ Object::~Object() { //cout << "Destructor called!\n"; //put it just to test objCounter--; totalArea -= this->area; areaMean = totalArea / objCounter; } 

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"; ///I have the get method implement origanally 

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.

+4
source share
2 answers

You need to define a copy constructor for your Object class. http://www.cplusplus.com/articles/y8hv0pDG/

+10
source

sudopunk is right for you, you need a copy constructor. However, it would be good practice to follow the rule of three and define a copy assignment operator:

If you have a destructor, copy constructor, or copy assignment operator, you must explicitly declare all three of them.

Here you can define them:

 Object& operator=(const Object& rhs); Object(const Object&); 

Note that copy constructors, copy assignment operators, and destructors are very important for any class that has members / resources that were allocated elsewhere - dynamically on the heap. Implicit copy constructors and assignment operators only copy objects in turn.

Another optimization is to apply the semantics of movement, but this is a different story, and this is beyond the scope of this thread.

+4
source

Source: https://habr.com/ru/post/1498597/


All Articles