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
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
Oh gods, why? ಥ_ಥ
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 03:50 PM.
but could you access the object later on or do you have to recreate it?
Oh gods, why? ಥ_ಥ
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.
<pre style="line-height: 8px;">joe = new Person("Joe", "Bloggs");
causes:
________ <u>0x0001 </u>
| 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:
________ <u>0x0001 </u>
| 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.
</pre>
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.
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
Oh gods, why? ಥ_ಥ
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:However, we can overload the + operator to execute code that actually add the two vectors together:PHP Code:
int arr1[3] = { 1, 2, 3 };
int arr2[3] = { 4, 5, 6 };
cout << arr1 + arr2; // Bad
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.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 numElements, int *values) // Constructor to assign vector values
{
vector = values;
size = numElements;
}
Vect Vect::operator+ (Vect vector1, Vect vector2) // Overloaded + operator
{
Vect added(vector1.size); // Creates a new vector to contain added values
for (int i = 0; i < vector1.size && i < vector2.size; i++) {
added[i] = vector1[i] + vector2[i];
}
return added;
}
}
// Then we can call this code:
Vect arr1(3, {1, 2, 3});
Vect arr2(3, { 4, 5, 6 });
cout << arr1 + arr2; // Good
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).![]()
hmmmm...... so you are basically making something up to manipulate the data? meh..... im not that smart. im probably gonna have issues with it. my main weakness has always been arrays and such. new concepts + arrays = x_x
Oh gods, why? ಥ_ಥ
Basically it's defining the way two objects behave when an operator is used on them. You can create an instance of an object such as a string, for example - what should happen when somebody says:In the rules of English, there's no default action that you should take when somebody applies addition to a string, so in C++ you can overload the operator and make the two strings actually do something useful when you add them. Like joining them (this is actually what C++ already does, but it's an example of how operators are overloaded.PHP Code:
string s("lol" + " internets");
).
Similarly, what should happen when you try to divide two strings? C++ doesn't define an action for string division, so you could choose whatever behaviour you like. You could return the lengths of the strings divided; you could return the first half of the first string matched up with the second half of the second string and vice versa; you could even do something like this:PHP Code:
string string::operator+ (string first, string second)
{
// Some code here to join both strings together and return them
}
Then, if someone tried to divide strings:PHP Code:
string string::operator/ (string first, string second)
{
return "That doesn't even make sense!";
}
PHP Code:
string s1("lol");
string s2("internets");
string s3 = s1/s2;
// The string "s3" now contains the string "That doesn't even make sense
oic!!!! XD thnx!
Oh gods, why? ಥ_ಥ