![]() |
| | >>> Click
here to download Final Fantasy Ringtones |
| |
#1 wtf are constructors and destuctors for?! omg im so confused!!! is it really neccesary to have Nutz() ~Nutz() if it doesnt do anything?! can someone explain this to me in lamemans terms? i be weetahded. i will lub you foreber |
| | |
| |
| | Constructors and destructors are basically exactly what their name implies. Constructors instantiate and create the object while destructors destroy the object. You have to remember that any variant of C is an object oriented language, so you have to have a certain mindset for it. Really, once you get used to it, these will become second nature and very, very easy. Last edited by Magixion; 08-21-2008 at 04:50 PM. |
| | |
| Site Staff | When you declare an object using the constructor, you now have a variable name as a pointer to the object data in in memory. When you call delete() on an object, this calls the destructor. The effect of this is to remove the data pointed to by the variable name, invalidating the pointer. In plain English, you should have one call to "delete" for every "new" used in object creation, and after you call delete, you can't access that object again. In this diagram, each box is some memory block, containing some data. The variable name in the first box points to the object in the second box. The 0x0001 is just an arbitrary memory address. joe = new Person("Joe", "Bloggs"); |
| | |
| | o_O's explanation is great. I'd also like to point out that objects do not have to be instantiated as pointers (although this is the most useful way to do so). When not using pointers, the constructor is called upon the implementation of an object, ie: class SomeClass { public: SomeClass() {/*do stuff */ }; ~SomeClass() {/*free stuff*/}; }; SomeClass someclassobject; // constructor would be called Once your program exits, the deconstructor will be called. Deconstructors are useful for freeing memory. For instance, say you have an object that represents the player and stores a bitmap full of animations for the player. The constructor could load the bitmap and the deconstructor could free the memory used by the bitmap. |
| | |
| Site Staff | Overloading is thankfully easier than understanding blocks of memory and pointers. ![]() Consider this scenario: You have a class that represents a vector, and you need to come up with a way to add them together using the "+" operator. You'd normally store a vector as an array of numbers, but if you try to add two arrays together, you'd get a compile-time error: PHP Code: PHP Code: P.S. I haven't tested that code or anything, so don't expect it to work first time (there's bound to be something funky going on with pointers and references there). ![]() |
| | |