new is a c++ keyword that allocates memory for you and returns a pointer to it. So, what the Crectangle() does is allocate an integer for width and an integer for height + asociates the pointer with those memory locations. Later, when we do *width and *height, * makes us acess the memory pointed by the variable, we ve're acessing an int.
I guess they used pointers to show you another example of pointer use; this could be done just as eazily by
ooo i see. i looked back over it, read the chapter on pointers again, and it all makes sense. helps keep the memory clear and improves the efficiency huh? guess ill never see much happen in my small "Area of a rectangle" program huh, but one day, one day! lol.
anyway, i understand now. one thing im wondering is when is when is an object destroyed? an example maybe? (using the example above)
I'm a beginner but my understanding that an object is destroyed (or its memory is deallocated) either when it goes out of scope or when you call delete on it. When this happens, if it has a destructor, such as yours above: CRectangle::~CRectangle(); then it that is called and its memory is deallocated. For example:
// pointer to classes example
#include <iostream>
usingnamespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area (void) {return (width * height);}
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle a, *b, *c;
CRectangle * d = new CRectangle[2];
b= new CRectangle;
c= &a;
a.set_values (1,2);
b->set_values (3,4);
d->set_values (5,6);
d[1].set_values (7,8);
cout << "a area: " << a.area() << endl;
cout << "*b area: " << b->area() << endl;
cout << "*c area: " << c->area() << endl;
cout << "d[0] area: " << d[0].area() << endl;
cout << "d[1] area: " << d[1].area() << endl;
delete[] d;
delete b;
return 0;
}
in the main, look at b specifically. b is a pointer, which points to a CRectangle object? where is the object? for example, if we do CRectangle a i know that the "a" variable 'holds' this new object, right? by doing b= new CRectangle; are we simply creating a new CRectangle object in the memory, and accessing it by a pointer? (just like we created the new int into the memory in the example you posted above this?).
i can see how it all works and i can get it to work..i just want to understand how its all working thats all. :)
when you do CRectangle a, the compiler allocates space, when you make a pointer, however, the compiler creates a pointer object (4B on x84, same as sizeof(int)) which holds an adress. Than, you allocate memory via new operator, and you get an adress. Think of it as buying land and later placing a building on it, but when you let the compiler allocate it for you you just get the building.
so then why does deleteing the pointer to the bit of memory that now holds our object also removes the object itself from memory? are they somehow attached to each other when we create a new object in this way? because from what i understand, unless we have another pointer and give it the same address as this object, the object becomes inaccessible. if we had
1 2 3 4
CRectangle *a,*b;
a = new RCectangle();
b = &a;
we can use both a and b to access the same object, in the same way, right? (a->area(); and b->area(); would return the same thing). if we then did:
delete a;
would the object a and b were both pointing at (only b now) still exist? and if so, i conclude that this sort of object is only every removed from memory if all pointers to it are deleted. (would i be correct to conclude that?)
Just imagine pointers to be normal ints. operator & gets the address of a variable, and since a is a pointer you dont need to know where it is, but where is the object it points to - its value.
when you deleted a you didnt delete the land, but you demolished the building - the same building that was on land b. Its wierd with lands here, so imagine an address of an building instead. One building had two adresses, and you ordered a demolition company to delete a building on adress a. There is nothing left on that adress now, and since b was used for that building too, it has nothing to point to now. Note that a still contains the address where the building once used to be, but that doesnt mean that you can build another one there, because you dont controll where the building company (new) builds, they only tell you where they've build the object you requested.
So, if you were to
1 2 3 4 5 6
CRectangle *a,*b;
a = new CRectangle();
b = a;
delete a;
a = new CRectangle();
B would still point to the old adress and would be useless. You'd have to do
b = a;