Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector doesn't create objects properly

Tags:

c++

stdvector

I've created a class with some static datas. Something like this:

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's constructor and destructor I made:

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 my intention was to know how many Objects were created, it's total area and the mean area. I tested it with simple statements like:

Object first;
Object second;
cout << Object::getObjCounter() << "\n"; 
///I have the get method implement origanally

And everything it's ok. Object class count number of instaces correctly. I tested using simple arrays:

Object test[ 10 ];
cout << Object::getObjCounter() << "\n";

Amazing... it works, as it should; I tested with 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 the stdout... I put flags in constructor and destructor to see why it's happening. And it shows me that when I use the vector statement shown, constructor is called just on, than the destructor is called in sequence!!!! Why???? Doesn't make sense to me! Could anybody explain this to me? In addition, could anybody help me in using vector to achieve the same effect that I have with simple arrays, which means: has a bunch of objects inside something, and counting it correctly? The thing is I need vectors functionality like remove and add elements, and resizing, but I don't want to reinvent wheel. Thanks in advance.

like image 601
Hugo Oshiro Avatar asked Dec 08 '22 13:12

Hugo Oshiro


2 Answers

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

like image 141
slaterade Avatar answered Dec 21 '22 02:12

slaterade


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

If you have either the destructor, copy constructor or copy assignment operator, you should explicitly declare all three of them.

Here's how you would define them:

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

Note that copy constructors, copy assignment operators and destructors very important for any class that has members/resources that were allocated somewhere else - dynamically on the heap. Implicit copy constructors and assignment operators only do a memberwise copy of the objects.

Another optimization would be to apply move semantics but that's a different story and it's out of the scope of this thread.

like image 21
Oleksiy Avatar answered Dec 21 '22 02:12

Oleksiy