![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: programming, start |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| ||||
| Well if you look at pointers on Pointers you will see all the information you need, from that page alone i think i made 6,7,8+ projects so i could see what they did, because i did so many projects its helped me to memorize it all near enough, that's probably my biggest tip for learning C++, make many projects for each bit and when you get too the later tutorials on things like pointers mix in your if statements and other things you have learnt. Hope that helped a bit. Lee. |
| |||
| Yup, constructor's are the definition on how to create a new object. Dim Chet as New Person() I used the New Keyword so it "Instantiated" a new Object of Type Person as Variable name Chet. Destructors are yup, clean memory, remove itself from memory so Garbage Collector can come sweep it up ![]() Whoops, i shoulid probably look what forum I'm in before I answer in a different programming langauge, hehe __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes Last edited by Lee : 09-21-2007 at 10:40 AM. Reason: Please edit your last post!!! :) |
| The Following User Says Thank You to ccoonen For This Useful Post: | ||
HelloWorld (09-23-2007) | ||
| |||
| If you don't need a destructor, don't use one. If you're not sure whether or not you need one right now, then you probably don't need one. (If you're not using the 'new' or 'delete' keywords anywhere, then thats usually a sign that you don't need a destructor). |
| |||
| Quote:
For example, imagine a linked list class. The linked list class will be solely responsible for creating links. When the linked list object dies, you want it to automatically destroy all the links, rather than requiring the user to do that for you. The destructor will (hopefully) have a call that cleans up all of the new'ed memory, and prevent that class from causing memory leaks in the program. Incidentally, one can never, and should never, call a destructor. A destructor is either invoked by a call to delete, delete[] on a dynamically created object (with new or new[]) or the destructor is invoked implicitly when an object goes "out of scope" (Or, for static objects, when the program ends..). You certainly should never have code which looks like my_object.~my_class(); (I believe that's a compiler error, but just in case its not, don't do it!). For most classes, the default, empty destructor is enough. A destructor cannot be overloaded, nor can it take any arguments. Sometimes, when creating polymorphic base classes, you may wish to create an empty virtual destructor, to ensure that objects used polymorphically can be properly cleaned up That's about all you need to do. A side note though, (You can stop reading here if you like..). There is a rule known as "the rule of 3" - whereby, if your class needs a destructor, then it probably needs a Copy Constructor and an overloaded Assignment Operator ( = ) too. The reason for this, is that dynamic memory "belonging" to the class does not get copied automatically when an instance of that class is copied. What would happen if you had no copy constructor/assignment operator, when the object is copied, is that both objects would "point" to the same dynamically created objects. Sometimes this behaviour is desirable, and is known as a "shallow" copy. More often though, a copy constructor/assignment operator will be used to duplicate all the data along with the copied object - That operation performs a "deep" copy. In general, its best to leave dynamic memory alone until its really necessary. There's much that you can do with the STL which will allow you to design classes without worrying about new/delete. Last edited by Bench : 09-28-2007 at 11:33 AM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |