473,788 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

basket of fruits in C++

5 New Member
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 2654
weaknessforcats
9,208 Recognized Expert Moderator Expert
All of your classes need copy constructors. The deep copy will happen automatically.
Jun 8 '07 #2
finengin
5 New Member
Given the following class structure:

class Fruit { };
class Apple : public Fruit { };
class Banana : public Fruit { };
class FruitBasket {
public:
FruitBasket();
~FruitBasket();
FruitBasket(con st 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
finengin
5 New Member
I tried the following

fruitBasket(con st fruitBasket& rhs) { int n = rhs.fruitVector .size();
fruitVector.res erve(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
finengin
5 New Member
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(bask et);
fruitBasket* assignBasket = basket;

Can someone help???? Thanks.
Jun 9 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your FruitBasket class looks like this:

class FruitBasket {
public:
FruitBasket();
~FruitBasket();
FruitBasket(con st FruitBasket& rhs);
private:
vector<Fruit *> fruitVector;
};
The only data element is a vector<Fruit*>.

If you make a copy of a FruitBasketObje ct, 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
1673
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 i use Relational Tables in MySQL. I have a table called Orders, which stores the CustomersID and also has an OrderID (this is an autoincrement integer)
4
1825
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 following format: Product Price Quantity prod1 £6.99 1 prod2 £5.99 2 Postage ...
3
1420
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 code what i need. Please please please could someone please provide me with help, or coding on how to add item details into a basket record, once the user clicks the add to basket link.
3
2926
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 code what i need. Please please please could someone please provide me with help, or coding on how to add item details into a basket record, once the user clicks the add to basket link. *** Sent via Developersdex http://www.developersdex.com...
3
1874
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 to offer shopping basket ONLY to those that are logged
2
1448
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 before they log and then they may log What is expected at that point? Merge the content of the baskets delete the old one or else
11
2004
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 basket it's bring automatically 1 extra product some time 2 or some time 3 i've not made a file called GLOBAL.asa for generate session but if some body add item into the basket it takes OrderID & using Session .
1
1348
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 another page and directly commits the item selected to a string and then you must press add to basket again. I would like to remove this second page and have item added directly to the basket but i do not know how to select this item specfically...
4
1898
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 basket it's bring automatically 1 extra product some time 2 or some time it's working fine i think so problem with GLOBAL.asa File that's not generating proper session variable GLOBAL.asa Sub Application_OnStart
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.