Can someone enlighten me on Deep Copy Problem?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 25
Reputation: Niner710 is an unknown quantity at this point 
Solved Threads: 0
Niner710 Niner710 is offline Offline
Light Poster

Can someone enlighten me on Deep Copy Problem?

 
0
  #1
Nov 6th, 2009
I have an function in which I want to make a copy of an object.

  1. ClassBase
  2. {
  3. public:
  4. Tblk myTblk;
  5. private:
  6.  
  7. };
  8.  
  9. void ClassBase::CopyObject(const ClassBase &test)
  10. {
  11. TblkPtr_t myTblk = TblkPtr_t(new Tblk); //TblkPtr_t is a typedef shared pointer
  12. *myTblk = *test.myTblk;
  13. }

For some reason that doesn't work. If I use the copy constructor like this

  1. myTblk = new Tblk(*test.myTblk)

then it seems to work. I don't understand why if I dereference the pointer in my original example why it wouldn't work. Can someone enlighten me?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,458
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #2
Nov 6th, 2009
Your copy ctor is hiding the member variable. And even worse you have
a memory leak.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 231 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC