473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

basket of fruits in C++

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
5 2629
weaknessforcats
9,208 Expert Mod 8TB
All of your classes need copy constructors. The deep copy will happen automatically.
Jun 8 '07 #2
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
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
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
9,208 Expert Mod 8TB
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

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

Similar topics

0
by: Ian N | last post by:
Hi, I've been working on a PHP Shopping Basket and have hit a problem. I wanted update the DB with the contents of the Array which forms the basket. This isn't a problem, however i'm unsure how...
4
by: Richard Pain | last post by:
OK - I have a shopping basket with data in a database. Once the person has successfully completed their order I want to be able to send them an email with the products ordered roughly in the...
3
by: nike | last post by:
please i need help. I am doing a wed site online shopping system for my school project, which requires some asp coding using vb script. I am new to asp and have no knowledge whatsoever on how to...
3
by: help | last post by:
please i need help. I am doing a wed site online shopping system for my school project, which requires some asp coding using vb script. I am new to asp and have no knowledge whatsoever on how to...
3
by: Samuel Shulman | last post by:
Hi I need to implement a shopping basket for my e-commerce website Scenarios are as follows 1. Website to offer shopping basket for all visitors including those who didn't log 2. Website...
2
by: Samuel Shulman | last post by:
I want to allow customer to 'Add to Basket' even if they are not logged. Once they log the content of the basket will be saved for the future Next time they may add other items to the basket...
11
Fary4u
by: Fary4u | last post by:
Hi i've design the ASP shopping with MS Access database it's working fine there is no problem in LOCALHOST when i upload into the Host Server it's working fine but when u add product into...
1
by: Sparkhill | last post by:
I have this page that prints out the contents of a table and commits it to a table. thus the string is full of all the data from the list. At the moment i have a add to basket button that redirect to...
4
Fary4u
by: Fary4u | last post by:
Hi for the last few months i've just stuck in 1 error & i don't know how to figer it out could any body find out where is the problem gonna be ? it's working fine but when u add product into...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.