Your FruitBasket class looks like this:
class FruitBasket {
public:
FruitBasket();
~FruitBasket();
FruitBasket(const FruitBasket& rhs);
private:
vector<Fruit *> fruitVector;
};
The only data element is a vector<Fruit*>.
If you make a copy of a FruitBasketObject, the copy will contain a copy of the vector, and with it, a copy of all the Fruit*. Whichever object deletes a Fruit* will screw up the other object.
Therefore: Do not make copies of pointers in C++.
Instead, make a decision:
1) If you want a copy of the Fruit objects, then make a copy of the Fruit objects in the FruitBasket copy constructor.
-
FruitBasket::FruitBasket(const FruitBasket rhs)
-
{
-
vector<Fruit*>::iterator itr = rhs.fruitVector.begin();
-
while (itr != rhs.fruitVector.end())
-
{
-
Fruit* temp = new Fruit(*itr);
-
this->fruitVector.push_back(temp);
-
++itr;
-
}
-
}
-
Of course, this assumes Fruit has a copy constructor.
2) If you do not want a copy of the Fruit but want to have both FruitBasket objects point at the same Fruit (as in a Fruit*), then use a vector of handles to Fruit.
For this, read the Handle Class article in the C/C++ Articles forum.
In any case, as a general rule, disaster awaits by making copies of objects that have pointer members wilthout a) making a copy of the object pointed at, or b) using a handle.