SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming
View Poll Results: What Languages Besides C++ Do You Use Copy Constructors In?
OO (object-oriented languages, such as Java) 1 100.00%
OB (object-based languages, such as JavaScript) 0 0%
All of Above 0 0%
None 0 0%
Voters: 1. You may not vote on this poll


Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 10th, 2003, 06:05 PM
viz viz is offline
::::
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 64 viz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Why Is There So Much Emphasis on Copy Constructors in C++?

While reading up on C++ a couple of nights ago as a refresher to help get through a book on OO development, I spent a good while reading about the Copy Constructor, and related topics like default constructors and shallow vs. deep copies. Anyhow, I couldn't help to start wondering just why is there so much emphasis on using copy constructors in C++?

All the other object-oriented and object-based languages I've studied and used (e.g., Java, PHP, ASP, JavaScript, ActionScript, Lingo) hardly seem to mention or use copy constructors; is there a reason for that?
__________________
Disclaimer: content may have handled during settling.
::::::::::::::
regards, viz

Reply With Quote
  #2  
Old May 10th, 2003, 07:01 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
One reason is that you must use them for classes that have dynamically allocated memory. There will be bugs in these classes if you do not define a proper copy constructor, and just use the default copy constructor provided by the language.

The problem arises in this particular case because the default copy constructor makes a copy of all variables within the class, and stores in them the same values as the original class. This means a pointer will be 'copied' by storing the same address into the newly created class. For dynamically allocated memory, this is a BIG problem, as the program will eventually attempt to de-allocate this same allocated memory twice. The first time will not be a problem... the second time will cause a run-time error.

Reply With Quote
  #3  
Old May 10th, 2003, 07:13 PM
viz viz is offline
::::
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 64 viz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Thanks Jason D., I don't have a problem understanding issues with multiple pointers using the same address, but how do other languages handle dynamically allocated memory for copied objects then? Do they all use 'deep copies' automatically, or something like that?

Reply With Quote
  #4  
Old May 11th, 2003, 06:00 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
Java, PHP, ASP, JavaScript, ActionScript, Lingo - they all donīt support pointers and direct memory allocation. So they donīt need special care when copying objects.
(Since when is Lingo a OOP language?)
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #5  
Old May 11th, 2003, 12:51 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 56 m 57 sec
Reputation Power: 437
Re: Why Is There So Much Emphasis on Copy Constructors in C++?

Quote:
Originally posted by viz
Anyhow, I couldn't help to start wondering just why is there so much emphasis on using copy constructors in C++?


Another reason for the importance of copy constructors is that (unless the new standard changed everything) the system will be making and destroying copies of your objects without your ever realizing it and it's going to need those copy constructors to do it right.

For example, it's deeply ingrained in me to always use a pointer or a reference to pass a struct or an object as a parameter to a function. However, if you pass an object to a function, then it will create a copy of the object for use within that function. If the copy is not done right, then that function may not function properly. And if the destruction is not done right, then you could have a memory leak on your hands.

There are other instances of automatic creation of object copies that I can't even remember anymore, but I do remember that Scott Myer's "Effective C++: 50 Specific Ways to Improve Your Programs and Design" did a good job of describing the reasons for copy constructors, virtual destructors, and deep vs shallow copies, etc.

Last edited by dwise1_aol : May 11th, 2003 at 12:53 PM.

Reply With Quote
  #6  
Old May 11th, 2003, 09:51 PM
viz viz is offline
::::
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 64 viz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
Originally posted by dwise1_aol
Another reason for the importance of copy constructors ...

...other instances of automatic creation of object copies that I can't even remember anymore, but I do remember that Scott Myer's "Effective C++: 50 Specific Ways to Improve Your Programs and Design" did a good job of describing the reasons for copy constructors, virtual destructors, and deep vs shallow copies, etc.
Thank you dwise1_aol, and also for the 'Effective C++' reference.
Quote:
Originally posted by M.Hirsch
Java, PHP, ASP, JavaScript, ActionScript, Lingo - they all donīt support pointers and direct memory allocation. So they donīt need special care when copying objects....
And thank you, M. Hirsch ... had a feeling there might be a simple reason for it, although when I took a quick look in the official PHP online documentation, the other day, I noticed that PHP does support some type of copy constructor. Do you happen to know what it's for? Haven't had time to check it out yet.
Quote:
Originally posted by M.Hirsch
...(Since when is Lingo a OOP language?)
In my experience Lingo has a good deal of OO capabilities, although I haven't used them enough recently to remember exactly how extensive they are.

I remember Lingo supported a special keyword for object creation, along with a system of inheritance, static members, and more. Director's GUI also supported reuse of a variety of 'Cast' member types (graphics, quicktime, text, audio, etc., as 'Sprites' (instances of Cast members), along with programmatic access to them. Don't remember any language access modifiers, but as I said, it's been awhile...

Reply With Quote
  #7  
Old May 12th, 2003, 01:15 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
OT: Lingo

Quote:
In my experience Lingo has a good deal of OO capabilities, although I haven't used them enough recently to remember exactly how extensive they are.

Probably those youīve seen are the only ones. It has no real OOP features, it only supports an OOP-similar style of writing the code. Just to make the code look "better than basic" But Lingo is one, if not the worst computer language I ever met. I have to work with it every other day, hear my words: Donīt spend any more minute on it than you really have to. Itīs really not worth it. Everytime you find a "cool feature", it wonīt work. Or it wonīt work in the Player. Or it wonīt work with your version of director. Or it wonīt work in conjunction with other functions / features. Or it will segfault randomly And if it actually does work, it will take you months to find the documentation about how to use it correctly (yes, I have both handbooks and also a third-party book, my first search is always on macromedia.com - most problems are bugs known since ages)

... just my 2ct about Lingo. Sounds frustrating? It is!

Reply With Quote
  #8  
Old May 12th, 2003, 03:42 PM
viz viz is offline
::::
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 64 viz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Re: OT: Lingo

Quote:
Originally posted by M.Hirsch
Probably those youīve seen are the only ones. It has no real OOP features, it only supports an OOP-similar style of writing the code. Just to make the code look "better than basic" But Lingo is one, if not the worst computer language I ever met....
Well, M.Hirsch, while I can certainly appreciate your desire to rant about Lingo (see below), what's your minimal definition of support to qualify a language as OO?

Actually I just did a search on Google, and it looks like I left out a slew of Lingo's more advanced OOP features, though it doesn't sound like you're interested to hear about them. I have heard it said that inheritance, which Lingo supports aplenty, and in several forms, two of which I mentioned in my post, is the MOST important feature of OO. I also remember picking up at least a couple of books maybe eight to ten years ago that went into great detail about OOP with Lingo, so not everyone shares your view.
Quote:
I have to work with it every other day, hear my words: Donīt spend any more minute on it than you really have to. Itīs really not worth it....... just my 2ct about Lingo. Sounds frustrating? It is!
I have worked on some projects that sound similar to yours, and I'd agree that they were frustrating. Frankly though, I thought the source of most of the confusion had less to do with Lingo itself than with the way the projects had been passed around from programmer to programmer, company to company; with the minimal documentation; and with lackadaisical attitudes by the project managers to supporting the projects.

Can you imagine starting a project on a cold weekend afternoon in October, only to find out at after nightfall that your workspace had no immediate lighting (all the directional stuff above was unadjustable and pointed the wrong way), no heat (the unit in my area was broken, and not repaired for about two weeks), a system located so far under the desk that I had to reach way under and poke around with a ruler to open and change CDs? (the system unit was also held in place by so much cabling and wires and surrounded by such quantities of boxes and junk on the back side that when I was finally able to take time to reposition it a couple of weeks later, that took a couple of hours). Anyway, that was just a tip on the iceberg, and I did also have to deal with unsupported, legacy Lingo, but ... now you've got me started too -- hope you're happy.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Why Is There So Much Emphasis on Copy Constructors in C++?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway