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 575147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:54:23+00:00 2026-05-13T13:54:23+00:00

I have the following C++ code (simplified version): class Shape { bool isCircle =

  • 0

I have the following C++ code (simplified version):

class Shape
{
    bool isCircle = false;
    bool isSquare = false;
}

class Circle : public Shape
{
    // some special members/methods
}

class Square : public Shape
{
    // some special members/methods
}

class CAD
{
    virtual DrawCircle(Circle * circle) = 0;
}

class SWX : public CAD
{
    virtual DrawCircle(Circle * circle){// do some stuff that draws circle on SWX system}
}

class PRO : public CAD
{
    virtual DrawCircle(Circle * circle){// do some stuff that draws circle on PRO system}
}

int main()
{
    Circle * circle = new Circle();
    circle->isCircle = true;

    Square * sq = new Square;
    sq->isSquare = true;

    vector<Shape*> shapes;
    shapes.push_back(circle);
    shapes.push_back(sq);

    SWX * swx = new SWX();

    for( int i = 0 ; i < shapes.size() ; ++i )
    {
        if( shapes[i]->isCircle )
    {
        SWX->DrawCircle((Circle*)(shapes[i]));
    }
    else if( shapes[i]->isSquare )
    {
        SWX->DrawSquare((Square*)(shapes[i]));
    }
}

I wish to remove the need for if…else (if at all possible within the constraints stated below).

My constraints right now are:

  • The CAD and derived classes are ginormous classes with various external dependencies.
  • The CAD classes cannot be merged with the Shape and derived classes (that would have been ideal, since then I can use polymorphism to solve my problem), since other projects/classes depend on the Shape classes and cannot depend on the CAD classes.
  • There are more than a dozen Shape-derived classes with a half dozen CAD-derived classes and this if…else is happening in numerous locations – so it would help if any solution is simple to understand (easier to convince my teammates to change legacy code).

Any suggestions/comments/solution you have would be most welcome.

  • 1 1 Answer
  • 2 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-13T13:54:23+00:00Added an answer on May 13, 2026 at 1:54 pm

    The standard solution for this problem, especially given your constraints regarding dependencies, is to use the Visitor Pattern.

    Here’s how Visitor Pattern would work in your case:

    • You need an abstract ShapeVisitor class. It has an abstract Visit method for each concrete subclass of Shape. eg: Visit(Circle*), Visit(Square*), etc.
    • Shape has an abstract AcceptVisitor(ShapeVisitor*) method.
    • Each Shape subclass implements AcceptVisitor as just calling visitor->Visit(this)
    • Each CAD class is a (or has-a, up to you) a ShapeVisitor. The Visit methods do the appropriate drawing for the specific type of Shape. No conditional or casting required.

    Here’s a modified version of your code that uses Visitor Pattern in a pretty low-impact way:

    class Circle;
    class Square;
    class ShapeVisitor
    {
        virtual void Visit(Circle *circle) = 0;
        virtual void Visit(Square *square) = 0;
    }
    
    class Shape
    {
        virtual void AcceptVisitor(ShapeVisitor *visitor) = 0;
    }
    
    
    class Circle : public Shape
    {
        // some special members/methods
    
        virtual void AcceptVisitor(ShapeVisitor *visitor)
        {
            visitor->Visit(this);
        }
    }
    
    class Square : public Shape
    {
        // some special members/methods
    
        virtual void AcceptVisitor(ShapeVisitor *visitor)
        {
            visitor->Visit(this);
        }
    }
    
    class CAD : public ShapeVisitor
    {
        virtual DrawCircle(Circle *circle) = 0;
        virtual DrawSquare(Square *square) = 0;
    
        virtual void Visit(Circle *circle) {
            DrawCircle(circle);
        }
    
        virtual void Visit(Square *square) {
            DrawSquare(square);
        }
    }
    
    class SWX : public CAD
    {
        virtual DrawCircle(Circle *circle){// do some stuff that draws circle on SWX system}
    
    }
    
    class PRO : public CAD
    {
        virtual DrawCircle(Circle * circle){// do some stuff that draws circle on PRO system}
    }
    
    int main()
    {
        Circle * circle = new Circle();
        Square * sq = new Square;
    
        vector<Shape*> shapes;
        shapes.push_back(circle);
        shapes.push_back(sq);
    
        SWX * swx = new SWX();
    
        for( int i = 0 ; i < shapes.size() ; ++i )
        {
            shapes[i]->AcceptVisitor(SWX);
        }
    }
    

    In this code I’ve opted for making CAD actually a subclass of ShapeVisitor. Also, since you’ve already got virtual methods in CAD to do the drawing, I implemented the Visit methods there (once), rather than once in each subclass. Once you switch clients over to the using AcceptVisitor instead of calling the Draw* methods directly you could make those methods protected, and then eventually move the implementation of the Visit methods down to the subclasses (that is: refactor to remove the extra level of indirection caused by having Visit(Foo*) call DrawFoo(Foo*)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is the simplified version of my problem: public partial class Form1
Consider the following simplified version of my code. I have a template class A
I am debugging some code and have encountered the following SQL query (simplified version):
If I have the following code (simplified version of the problem): class TestA {
I have the following code (I simplified it & removed unrelevant parts) public class
I got the following code for my object (short version) : public class PluginClass
I have a code a following (simplified version): #define MESSAGE_SIZE_MAX 1024 #defined MESSAGE_COUNT_MAX 20
I have the following function in my code. Here's a simplified version: $.ajax({ url:
Imagine I have the following code (simplified regarding my real context of course): <div
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>

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.