|
||
|
|
#1 (permalink) |
|
Forum Expert
|
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;
}
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? Last edited by milt; 03-28-2007 at 03:23 PM. |
|
|
|
|
|
#4 (permalink) |
|
Forum Expert
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|