473,394 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Container with Abstract Type

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

Jul 23 '05 #1
3 1733
* 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?
Jul 23 '05 #2

"pmatos" <po**@sat.inesc-id.pt> wrote in message
news:11*********************@g14g2000cwa.googlegro 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

Jul 23 '05 #3
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
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Markus Dehmann | last post by:
I defined a base class in order to put heterogeneous values into a standard container: All values that I store in the container are derived from my base class. Now when I iterate over the...
6
by: Aguilar, James | last post by:
Suppose I have my own template container that represents a matrix. The template's only parameter is < typename T > for the type that will go into it. The only problem is that calling this...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
0
by: Craig Buchanan | last post by:
i am trying to build an application that uses plugins to extend the business functionality of an application. i've decided to use inheritance of abstract classes as the mechanism to do this. ...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
3
by: orkonoid | last post by:
Hello, I am having trouble with a Polymorphism issue using container classes. I have a longwinded and shortwinded version of my question: Shortwinded version: How can I store base class...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.