Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 648561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:50:47+00:00 2026-05-13T21:50:47+00:00

Let’s say we already have a hierarchy of classes, e.g. class Shape { virtual

  • 0

Let’s say we already have a hierarchy of classes, e.g.

class Shape { virtual void get_area() = 0; };
class Square : Shape { ... };
class Circle : Shape { ... };
etc.

Now let’s say that I want to (effectively) add a virtual draw() = 0 method to Shape with appropriate definitions in each sub-class. However, let’s say I want to do this without modifying those classes (as they are part of a library that I don’t want to change).

What would be the best way to go about this?

Whether or not I actually “add” a virtual method or not is not important, I just want polymorphic behaviour given an array of pointers.

My first thought would be to do this:

class IDrawable { virtual void draw() = 0; };
class DrawableSquare : Square, IDrawable { void draw() { ... } };
class DrawableCircle : Circle, IDrawable { void draw() { ... } };

and then just replace all creations of Squares and Circles with DrawableSquares and DrawableCircles, respectively.

Is that the best way to accomplish this, or is there something better (preferably something that leaves the creation of Squares and Circles intact).

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:50:48+00:00Added an answer on May 13, 2026 at 9:50 pm

    (I do propose a solution down further… bear with me…)

    One way to (almost) solve your problem is to use a Visitor design pattern. Something like this:

    class DrawVisitor
    {
    public:
      void draw(const Shape &shape); // dispatches to correct private method
    private:
      void visitSquare(const Square &square);
      void visitCircle(const Circle &circle);
    };
    

    Then instead of this:

    Shape &shape = getShape(); // returns some Shape subclass
    shape.draw(); // virtual method
    

    You would do:

    DrawVisitor dv;
    Shape &shape = getShape();
    dv.draw(shape);
    

    Normally in a Visitor pattern you would implement the draw method like this:

    DrawVisitor::draw(const Shape &shape)
    {
      shape.accept(*this);
    }
    

    But that only works if the Shape hierarchy was designed to be visited: each subclass implements the virtual method accept by calling the appropriate visitXxxx method on the Visitor. Most likely it was not designed for that.

    Without being able to modify the class hierarchy to add a virtual accept method to Shape (and all subclasses), you need some other way to dispatch to the correct draw method. One naieve approach is this:

    DrawVisitor::draw(const Shape &shape)
    {
      if (const Square *pSquare = dynamic_cast<const Square *>(&shape))
      {
        visitSquare(*pSquare);
      }
      else if (const Circle *pCircle = dynamic_cast<const Circle *>(&shape))
      {
        visitCircle(*pCircle);
      }
      // etc.
    }
    

    That will work, but there is a performance hit to using dynamic_cast that way. If you can afford that hit, it is a straightforward approach that is easy to understand, debug, maintain, etc.

    Suppose there was an enumeration of all shape types:

    enum ShapeId { SQUARE, CIRCLE, ... };
    

    and there was a virtual method ShapeId Shape::getId() const = 0; that each subclass would override to return its ShapeId. Then you could do your dispatch using a massive switch statement instead of the if-elsif-elsif of dynamic_casts. Or perhaps instead of a switch use a hashtable. The best case scenario is to put this mapping function in one place, so that you can define multiple visitors without having to repeat the mapping logic each time.

    So you probably don’t have a getid() method either. Too bad. What’s another way to get an ID that is unique for each type of object? RTTI. This is not necessarily elegant or foolproof, but you can create a hashtable of type_info pointers. You can build this hashtable in some initialization code or build it dynamically (or both).

    DrawVisitor::init() // static method or ctor
    {
      typeMap_[&typeid(Square)] = &visitSquare;
      typeMap_[&typeid(Circle)] = &visitCircle;
      // etc.
    }
    
    DrawVisitor::draw(const Shape &shape)
    {
      type_info *ti = typeid(shape);
      typedef void (DrawVisitor::*VisitFun)(const Shape &shape);
      VisitFun visit = 0; // or default draw method?
      TypeMap::iterator iter = typeMap_.find(ti);
      if (iter != typeMap_.end())
      {
        visit = iter->second;
      }
      else if (const Square *pSquare = dynamic_cast<const Square *>(&shape))
      {
        visit = typeMap_[ti] = &visitSquare;
      }
      else if (const Circle *pCircle = dynamic_cast<const Circle *>(&shape))
      {
        visit = typeMap_[ti] = &visitCircle;
      }
      // etc.
    
      if (visit)
      {
        // will have to do static_cast<> inside the function
        ((*this).*(visit))(shape);
      }
    }
    

    Might be some bugs/syntax errors in there, I haven’t tried compiling this example. I have done something like this before — the technique works. I’m not sure if you might run into problems with shared libraries though.

    One last thing I’ll add: regardless of how you decide to do the dispatch, it probably makes sense to make a visitor base class:

    class ShapeVisitor
    {
    public:
      void visit(const Shape &shape); // not virtual
    private:
      virtual void visitSquare(const Square &square) = 0;
      virtual void visitCircle(const Circle &circle) = 0;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.