Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Container with Abstract Type

Reply
Thread Tools

Container with Abstract Type

 
 
pmatos
Guest
Posts: n/a
 
      03-03-2005
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-03-2005
* 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;

See <url: http://www.boost.org/>.


> (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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      03-03-2005

"pmatos" <(E-Mail Removed)-id.pt> wrote in message
news:(E-Mail Removed) ups.com...
> 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



 
Reply With Quote
 
Matthias Kaeppler
Guest
Posts: n/a
 
      03-03-2005
pmatos wrote:
> 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.

--
Matthias Kaeppler
 
Reply With Quote
 
 
 
Reply

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract Methods & Abstract Class Iyer, Prasad C Python 0 10-20-2005 06:35 AM
About abstract class and abstract method Sameer Java 4 08-31-2005 12:59 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM
Abstract class with no abstract functions Uzytkownik C++ 3 04-03-2005 05:45 PM
Abstract Classes w/o abstract methods DaKoadMunky Java 4 04-20-2004 04:53 AM



Advertisments