|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
| 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 | |||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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.
__________________
Jason Doucette / Xona.com - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#3
|
|||
|
|||
|
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?
|
|
#4
|
|||
|
|||
|
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. |
|
#5
|
||||
|
||||
|
Re: Why Is There So Much Emphasis on Copy Constructors in C++?
Quote:
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. |
|
#6
|
|||||
|
|||||
|
Quote:
![]() Quote:
Quote:
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... ![]() |
|
#7
|
|||
|
|||
|
OT: Lingo
Quote:
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! |
|
#8
|
||||
|
||||
|
Re: OT: Lingo
Quote:
![]() 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:
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. ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Why Is There So Much Emphasis on Copy Constructors in C++? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|