Container with Abstract Type

Discussion in 'C++' started by pmatos, Mar 3, 2005.

  1. pmatos

    pmatos Guest

    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
     
    pmatos, Mar 3, 2005
    #1
    1. Advertising

  2. * 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?
     
    Alf P. Steinbach, Mar 3, 2005
    #2
    1. Advertising

  3. pmatos

    Howard Guest

    "pmatos" <-id.pt> wrote in message
    news:...
    > 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
     
    Howard, Mar 3, 2005
    #3
  4. 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
     
    Matthias Kaeppler, Mar 3, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky
    Replies:
    4
    Views:
    557
    Lee Weiner
    Apr 20, 2004
  2. Matthias Kaeppler
    Replies:
    1
    Views:
    458
    R.F. Pels
    May 22, 2005
  3. Sameer
    Replies:
    4
    Views:
    613
    Roedy Green
    Aug 31, 2005
  4. Uzytkownik
    Replies:
    3
    Views:
    601
    Uzytkownik
    Apr 3, 2005
  5. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    541
    Iyer, Prasad C
    Oct 20, 2005
Loading...

Share This Page