943,931 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 467
  • C++ RSS
Nov 6th, 2009
0

Can someone enlighten me on Deep Copy Problem?

Expand Post »
I have an function in which I want to make a copy of an object.

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Niner710 is offline Offline
31 posts
since Jul 2008
Nov 6th, 2009
0
Re: Can someone enlighten me on Deep Copy Problem?
Your copy ctor is hiding the member variable. And even worse you have
a memory leak.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++, Visual C++ or Java for Win32API firewall application?
Next Thread in C++ Forum Timeline: Is .NET good option to make a GUI in C++?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC