Advertisement

C++ makes me confused!

Started by June 08, 2002 06:15 PM
3 comments, last by the Chef 22 years, 6 months ago
I find this code a little confusing:
    
#include <iostream.h>

class Shape {
public:
    Shape(int Width, int Height) : itsWidth(Width), itsHeight(Height);
    virtual ~Shape();

    virtual int getArea() { return itsWidth * itsLength; }
protected:
    int itsHeight;
    int itsWidth;
}

class Square : public Shape {
public:
    Square(int Side) : itsSide(Side);
    ~Square();

    int getArea() { return Side * Side; }
private:
    int itsSide;
}

int main() {
    // I dont understand this part //

    Shape *ptr = new Square;
return 0;
}
    
I dont understand this pointer! What is stored in the RAM? How do i accses the methods? I dont get a thing!! Please help! [edited by - the Chef on June 8, 2002 7:17:03 PM]
No way! I will never write a profile signature! :p
Shape *ptr = new Square;
That is dynamicly creating an instance of the Square class at runtime of the base class, you woulndnt really need to do this with only one derived class as its use mainly for choosing which class to make an instance of during runtime.
You can use its methods like this:
(*ptr).getArea(); // as the dot is evaluated before the *
// operator and as this looks ugly they made
// it so you could do this:
ptr->getArea()

CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Advertisement
Well, ptr is simply a block of memory equal in size to the processor's word size. In the case of a modern x 86 processor, that's 32 bits. This is true of all pointers, regardless of what they point to because all addresses are stored in 32 bits (on x 86, at least). Now, new Square allocates a block of memory that holds a Square object (or "instance"). That block of memory will be equal in size to the value returned by the expression sizeof(Square) (plus a bit more if there's a virtual method table in there, but you needn't worry about that). ptr is made to hold that object's address.

Now, that code is very bad because you've allocated memory but not deallocated it. This is like having taken a crap but not wiping your ass before continuing about your business. So, when you're done with your Square object, delete it using the expression delete ptr —in this example you're saying, "deallocate the object pointed to by ptr " And if you'd have allocated an array as in int* array = new int[20] you'd deallocate that using delete[] array . delete is only for deallocating objects allocated with new , and delete[] is only for deallocating objects allocated with new[] .

By using the new operator in any form, you're requesting memory at runtime, and you should never do this unless you have some real need to, like you don't know how many objects you'll need. So, you can allocate the object on the stack just like you would with an int . This is very much unlike Java and C#, which do not allow stack allocation for object types. But C++ does. So all you need to do is Square mySquare (in the same fashion as int myint ) to create a Square instance. That way, you don't have to worry about memory leaks or pointers.

If you want to invoke the object's method (in your example), you have to keep something in mind: you only have a pointer to the object. To use an object through a pointer, you have to "dereference" it, which means that you tell the computer to give you the object the pointer is pointing to. You use the unary * operator to do this: *ptr . So, that gives you a Shape object. Now select the member: (*ptr).getArea() . Now, I had to put the dereferencing of ptr in parenthesis to ensure that this happens before the method is selected. A shorthand way of doing this is ptr->getArea() , and it does exactly the same thing. Now, if you'd allocated the object on the stack (like you should), you'd just mySquare.getArea() to invoke the method.


[edited by - merlin9x9 on June 8, 2002 8:43:04 PM]
merlin, I''ve never heard memory leaks described quite like that before. Personally I think it''s a good description.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Gracias.

This topic is closed to new replies.

Advertisement