Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 10 Nov 2003
Offline Last Active Nov 27 2010 03:04 PM

#316185 What is the purpose of creating a copy constructor?

Posted by on 27 April 2005 - 12:21 PM

I know this is a really newbie question, but please help me out. I've been looking all over the place about this. Supposedly, when creating an object from another object or when passing an object by value as a function argument and using the default copy c-tor, a shallow copy is made which ends up meaning that the two objects point to the same location in memory. When one loses scope the other is deleted (or something like that). This is solved by creating a constructor whose only argument is a reference to an instance of the class. For one, I don't really see why this suddenly solves the problem. The tutorials I looked at just ended there. It seems to me that you would have to use the extra constructor by first instancing the object and then assigning it as an argument to the new copy-constructor, or create a static function that returns this. Can someone explain a less roundabout way of doing this, or why it's really necissary in the first place? The only reason I'm looking at this is because I want to create a singleton and all of the tutorials I could find about singletons used a copy constructor (and I must know how each part of my program is working). Edit: Fixed some spelling errors.