Welcome to Eyes on FF!
>>> Click here to download Final Fantasy Ringtones
Oh no!
 

Post New Thread  Reply
 
LinkBack Thread Tools
NeoTifa
touche moi juste comme ça
NeoTifa's Avatar
Location: in your basement
#1
Default can somebody help me not suck at c++ plzkthnx

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
Old 08-21-2008, 05:28 AM
Reply With Quote
NeoTifa is offline  
Magixion
Magixion's Avatar
Location: The World of Ruin! Noooo!
Default

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.
Old 08-21-2008, 07:06 AM
Reply With Quote
Magixion is offline  
Flying Mullet
Site Staff
Cid's Knight
*this space for rent*
Flying Mullet's Avatar
Location: Finding my monkey cage exit strategy
Default

And don't beat yourself up too much over C++ being hard. It's one of the most complex OO languages to learn.
Old 08-21-2008, 03:01 PM
Reply With Quote
Flying Mullet is offline  
NeoTifa
touche moi juste comme ça
NeoTifa's Avatar
Location: in your basement
Default

but could you access the object later on or do you have to recreate it?
Old 08-23-2008, 01:50 AM
Reply With Quote
NeoTifa is offline  
o_O
Site Staff
nerd
o_O's Avatar
Location: New Zealand
Default

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");
causes:
________ 0x0001
| joe: | | firstname: "Joe" | Memory taken off of the "safe to use" pile and
| 0x0001 | ==> | surname: "Bloggs" | Joe's data stored in it.
|________| |____________________|



delete joe;
causes:
________ 0x0001
| joe: | | Previous memory | Memory gets returned to the "safe to use" pile
| 0x0001 | ==> | of joe but memory | and other objects may now occupy it. For this reason,
|________| | is now available | referring to an object after it's been deleted could
| to other objects | mean you're referring to some other object unknowingly
|____________________| and is therefore unsafe.
Old 08-23-2008, 07:14 AM
Reply With Quote
o_O is offline  
Moon Rabbits
so gangsta it hurrrrts..
Moon Rabbits's Avatar
Location: Caaaaaaaanada.
Default

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.
Old 08-25-2008, 07:28 AM
Reply With Quote
Moon Rabbits is offline  
NeoTifa
touche moi juste comme ça
NeoTifa's Avatar
Location: in your basement
Default

ok. a week ago that wouldve confused me cuz i just got passed the part in the book with pointers and free space. im starting to understand constructors now thanks. but what is getting me now is overloading operators XD
Old 08-28-2008, 07:01 PM
Reply With Quote
NeoTifa is offline  
o_O
Site Staff
nerd
o_O's Avatar
Location: New Zealand
Default

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:
int arr1[3] = { 12};
int arr2[3] = { 45};

cout << arr1 arr2// Bad 
However, we can overload the + operator to execute code that actually add the two vectors together:
PHP Code:
// Note: Calling the class "Vect" since there are already Vector libraries for C++ called "Vector" :p
class Vect
{
    
int vector[];    // This array will hold the values in the vector
    
int size;
    
Vect::Vect(int numElementsint *values)    // Constructor to assign vector values
    
{
        
vector values;
        
size numElements;
    }
    
    
Vect Vect::operator+ (Vect vector1Vect vector2)    // Overloaded + operator
    
{
        
Vect added(vector1.size);    // Creates a new vector to contain added values
        
for (int i 0vector1.size && vector2.sizei++) {
            
added[i] = vector1[i] + vector2[i];
        }
        return 
added;
    }
}

// Then we can call this code:
Vect arr1(3, {123});
Vect arr2(3, { 45});

cout << arr1 arr2// Good 
In plain English, overloading operators allow to to make two objects do whatever you like when you use some operator on them, e.g. obj1 + obj2; obj1 * obj2, etc.
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).
Old 08-30-2008, 01:13 AM
Reply With Quote
o_O is offline  
Post New Thread  Reply


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:40 PM.

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.0.0
Copyright ©2000 - 2007, Eyes on Final Fantasy.
Sean Robinson Design

Online Games | Scholarships | Tool Enhancers