Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 03-28-2007, 03:16 PM   #1 (permalink)
Forum Expert
 
milt's Avatar
 
Join Date: Nov 2003
Location: Lancaster, PA
Age: 20
Posts: 1,606
Send a message via AIM to milt Send a message via Yahoo to milt Send a message via Skype™ to milt
Default Polymorphism

Okay, so I'm back with another question about C++!

I've been working with polymorphism quite a bit lately, and I have run into a problem I'm trying to solve. Consider the following example.

Code:
class Shape
{
public:
    virtual void Draw() = 0;
};

class Shape2D : Shape
{
public:
    virtual void Area() = 0;
};

class Triangle : Shape2D
{
public:
    virtual void Draw()
    {
        printf( "Drawing a triangle..." );
    }
    virtual void Area()
    {
        printf( "Returning area" );
    }
};

class Rectangle : Shape2D
{
public:
    virtual void Draw()
    {
        printf( "Drawing a rectangle..." );
    }
    virtual void Area()
    {
        printf( "Returning area" );
    }
};

int main()
{
    Triangle tri;
    Rectangle rec;

    Shape2D *shapes = (Shape2D*)malloc( sizeof(Shape2D) * 2 );

    shapes[0] = tri;
    shapes[1] = rec;

    free( shapes );

    return 0;
}
Okay, so basically in this code I am trying to make an array of an abstract class, Shape2D. Although I realize that you can not make an instance of an abstract class, I'm basically wondering if you can have an array of them, which points to the derrived classes.

I'm getting compiler errors with that code, telling me that the type cast exists, but it is inaccessible. When I try to cast it as a Shape2D, I then get errors because it is an abstract class.

Any ideas?
__________________
--Milt, AKA Pokey

Last edited by milt; 03-28-2007 at 03:23 PM.
milt is offline   Reply With Quote
Old 03-28-2007, 04:18 PM   #2 (permalink)
Forum Expert
 
TheOutkastDev's Avatar
 
Join Date: Sep 2002
Location: Houston, Texas
Age: 22
Posts: 3,933
Default

Have you tried using static_cast?
TheOutkastDev is offline   Reply With Quote
Old 03-28-2007, 04:30 PM   #3 (permalink)
Forum Expert
 
milt's Avatar
 
Join Date: Nov 2003
Location: Lancaster, PA
Age: 20
Posts: 1,606
Send a message via AIM to milt Send a message via Yahoo to milt Send a message via Skype™ to milt
Default

Yes, it's giving me errors that it can't convert it to an abstract class. I don't think it's possible to do any type of cast to an abstract type.
__________________
--Milt, AKA Pokey
milt is offline   Reply With Quote
Old 03-29-2007, 03:36 PM   #4 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Ok, first off, the error that you're getting is because Rectangle and Triangle derive privately from Shape2D and to up-cast a derived class to its base class, it needs to be derived publicly from it (by not explicitly mentioning public, private or protected when deriving, it defaults to deriving privately).

Next, what you're trying to do is actually incorrect. You're trying to create an array of Shape2D's. This doesn't make sense because Shape2D is an abstract class, thus having an array of Shape2D instances would be just like creating 'x' many Shape2D's as variables. What you want in this case to store Triangles and Rectangles as Shape2D's is an array of pointer to Shape2D's (so Shape2D *[] or Shape2D **) as then those pointers could point to instances derived from Shape2D. Instances of objects (Rectangle's, not Rectangle *'s, for example) can't hold instances of derived classes, only pointers (or references) can do that.

Also, not as a matter of style but as a matter of general correctness, as a general rule of thumb for now, never use malloc() in C++ unless you actually want to allocate memory for some purpose (an actual array of bytes, not to actually be objects) or are allocating space for a simple type (such as int, float, etc, or class types that have no constructors or only a trivial constructor, do not derive from anything with more than a trivial constructor and do not have any virtual methods). The reasoning behind this is that when you use 'new' to create instances of classes on the heap, it calls the constructor for the class after allocating the memory. By calling malloc(), you're just allocating the memory and then using an unconstructed object. So, basically, just use new.

Last edited by Sep102; 03-29-2007 at 06:48 PM.
Sep102 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5