Container with Abstract Type

P

pmatos

Hi all,

I'm having a design problem. Imagine a Shape class that can be a
Square, a Rectangle or a Triangle. Shape should be an abstract class.
Now, I want to create a stack of Shapes.
I do
stack<Shape> shapeStack;

For some reason (probably because Shape is abstract) this is not
working, it results in a compile time error. How can I solve this
issue?

(Now, another question, if a class has at least a virtual member, does
it need to have a virtual destructor? If yes, why?)

Cheers,

Paulo Matos
 
A

Alf P. Steinbach

* pmatos:
I'm having a design problem. Imagine a Shape class that can be a
Square, a Rectangle or a Triangle. Shape should be an abstract class.
Now, I want to create a stack of Shapes.
I do
stack<Shape> shapeStack;

For some reason (probably because Shape is abstract) this is not
working, it results in a compile time error. How can I solve this
issue?

Create a stack of non-abstract things. The most fundamental way is

stack<Shape*> shapeStack;

where each element of the stack is a pointer to a non-abstract object.

To automate destruction of those objects you can use boost::shared_ptr,

typedef boost::shared_ptr<Shape> ShapePtr;
stack<ShapePtr> shapeStack;

(Now, another question, if a class has at least a virtual member, does
it need to have a virtual destructor? If yes, why?)

No, it does not necessarily need a virtual destructor. However, there's
essentially no added cost for the virtual destructor then, and it allows
objects to be destroyed without knowing the exact type. Which you need
for e.g. your shapeStack.

If you define a destructor you probably also need to define a copy
constructor and an assignment operator, or make those private and
unimplemented.

This is commonly called the "rule of three": given that you need one of
them, you probably need (to take charge of) all of them.
 
H

Howard

pmatos said:
Hi all,

I'm having a design problem. Imagine a Shape class that can be a
Square, a Rectangle or a Triangle. Shape should be an abstract class.
Now, I want to create a stack of Shapes.
I do
stack<Shape> shapeStack;

For some reason (probably because Shape is abstract) this is not
working, it results in a compile time error. How can I solve this
issue?

It's apparent from your description that you intend to use these objects
polymorphically. The only way to accomplish that is via pointers (or
references). So your stack should be

stack<Shape*> shapeStack;

(Or better yet, use smart pointers (i.e., from Boost), and you'll save all
the new/delete headaches you might otherwise experience!)
(Now, another question, if a class has at least a virtual member, does
it need to have a virtual destructor? If yes, why?)

Not neccessarily, but if you're going to use pointers to base class objects
as I've suggested above, then the answer is yes, the destructor should be
virtual. Otherwise, you won't get proper polymorphic behavior when calling
delete via the base class pointer.

-Howard
 
M

Matthias Kaeppler

pmatos said:
Hi all,

I'm having a design problem. Imagine a Shape class that can be a
Square, a Rectangle or a Triangle. Shape should be an abstract class.
Now, I want to create a stack of Shapes.
I do
stack<Shape> shapeStack;

For some reason (probably because Shape is abstract) this is not
working, it results in a compile time error. How can I solve this
issue?

The point is, you must not instantiate abstract types, but with a stack
of Shapes, you're basically doing exactly that (storing Shape objects --
which would need to instantiate the abstract Shape type, which is
forbidden).

As already said, use pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top