By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,795 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,795 IT Pros & Developers. It's quick & easy.

basket of fruits in C++

P: 5
I have a basket of fruits (e.g. Apple, Orange, Banana, etc) in C++ and I want to "copy" its content into a different basket (deep copy). What issues should I be aware of and how to do it?? Thanks.
Jun 8 '07 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
All of your classes need copy constructors. The deep copy will happen automatically.
Jun 8 '07 #2

P: 5
Given the following class structure:

class Fruit { };
class Apple : public Fruit { };
class Banana : public Fruit { };
class FruitBasket {
public:
FruitBasket();
~FruitBasket();
FruitBasket(const FruitBasket& rhs);
private:
vector<Fruit *> fruitVector;
};

Can someone suggest the implementation of the destructors and copy constructors, such as the deletion of Fruit pointers in the destructor. Thanks, newbie to C++.
Jun 8 '07 #3

P: 5
I tried the following

fruitBasket(const fruitBasket& rhs) { int n = rhs.fruitVector.size();
fruitVector.reserve(n);
for (int i = 0; i< n; ++i) fruitVector[i] = rhs.fruitVector[i];
}

But the result is still shallow copy, can someone suggest a way to do deep copy??? Thanks.
Jun 8 '07 #4

P: 5
I also discovered that the copy constructor and assignment operator are not called for the following statements below:

assume basket is populated with fruit information:

fruitBasket* copyBasket(basket);
fruitBasket* assignBasket = basket;

Can someone help???? Thanks.
Jun 9 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
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.

Expand|Select|Wrap|Line Numbers
  1. FruitBasket::FruitBasket(const FruitBasket rhs)
  2. {
  3.     vector<Fruit*>::iterator itr = rhs.fruitVector.begin();
  4.     while (itr != rhs.fruitVector.end())
  5.     {
  6.           Fruit* temp = new Fruit(*itr);
  7.           this->fruitVector.push_back(temp);
  8.           ++itr;
  9.     }
  10. }
  11.  
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.
Jun 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.