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

  • SEARCH
  • Home
  • 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 8557891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:40:43+00:00 2026-06-11T15:40:43+00:00

I realize this is a very specific question so it would be helpful if

  • 0

I realize this is a very specific question so it would be helpful if the answer people give includes explicit codes on how to do this. Thanks.

I have an abstract base class Shape:

class Shape
{
    .....
    virtual bool GetIntersection(Shape* _shape) = 0;
}

class Circle : public Shape  {...}
class Triangle : public Shape {...}

Both these derived classes overrides GetIntersection;

I have:

//main.cpp
....
Shape* shape;
Shape* circle = new Circle;

if(input == 0) shape = new Circle;
else shape = new Triangle;

circle->GetIntersection(shape);

which gives an error.

I read something about visitor patterns and think that this might be the way to solve my problem as I basically need to determine which derived class the parameter to GetIntersection is using. Can someone explain how I would implement a visitor pattern for this case? Or if there is another simpler solution to this problem.

Any help would be appreciated.

  • 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-06-11T15:40:45+00:00Added an answer on June 11, 2026 at 3:40 pm

    I’m right now facing the same problem with collisions between different shapes.

    I think we cannot use the Visitor pattern “as is”, because it requires that the classes of a hierarchy A (visitors) visit the classes of another hierarchy B (visited elements), where the classes of B only know about the abstraction of A (IVisitor, for example) while the classes of B know about the subclasses of A (VisitedElement1, VisitedElement2… subclasses of VisitedElement).

    In our case, we are trying to visit Shapes with Shapes while keeping our Shape class decoupled from the subclasses, so the Visitor pattern doesn’t apply.

    The best solution I can think of is that the Shape class, directly or through another interface, declares “GetSpecificIntersection” methods for all the subtypes:

    class Shape
    {
        .....
    public:
        virtual bool GetIntersection(Shape* _shape) = 0;    
    protected:
        virtual bool GetSpecificIntersection(Circle* _circle) = 0;
        virtual bool GetSpecificIntersection(Triangle* _triangle) = 0;
    }
    
    class Circle
    {
        .....
    public:
        virtual bool GetIntersection(Shape* _shape);
        {
            return _shape->GetSpecificIntersection(this);
        }
    protected:
        virtual bool GetSpecificIntersection(Circle* _circle) { ... }
        virtual bool GetSpecificIntersection(Triangle* _triangle) { ... }
    }
    
    class Triangle { /* analog to Circle */ }
    

    Therefore, you must implement those specific intersection methods for each kind of shape to any other kind of shape and the Shape class is coupled to all the possible shapes. If I’m not wrong, this violates OCP but preserves LSP.

    Another option I’ve just come up with is to “favor composition over inheritance” and make something like this:

    class Shape
    {
        .....
    public:
        virtual bool GetIntersection(Shape* _shape)
        {
            return _shape->figure->GetIntersection(this->figure);
        }
    
    private:
        Figure* figure;
    }
    
    class Figure
    {
        .....
    public:
        virtual bool GetIntersection(Figure* _figure) = 0;
    protected:
        virtual bool GetSpecificIntersection(Circle* _circle) = 0;
        virtual bool GetSpecificIntersection(Triangle* _triangle) = 0;
    }
    
    class Circle
    {
        .....
    public:
        virtual bool GetIntersection(Figure* _figure)
        {
            return _figure->GetSpecificIntersection(this);
        }
    protected:
        virtual bool GetSpecificIntersection(Circle* _circle) { ... }
        virtual bool GetSpecificIntersection(Triangle* _triangle) { ... }
    }
    
    class Triangle { /* analog to Circle */ }
    

    It’s the same as before, but by doing this you decouple your Shape class (which is the interface used by the classes of your game) from the different “Figures” (I didn’t come up with a better name), so adding new Figures shouldn’t even lead to recompile your client classes. This violates OCP only in the “Figures” hierarchy, and preserves LSP for both “Shape” and “Figure”.

    If someone can suggest a better way to do this while avoiding downcasting, I’ll be very thankful 😀

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

Sidebar

Related Questions

I realize this is a very specific question, and not very helpful outside of
I realize this is more of a hardware question, but this is also very
This is a very elementary I realize, I have recently started working with asp.net
I realize this is a basic question but I have searched online, been to
I realize this question is pretty basic, but I'm really stuck. I have a
I realize how simple this question should be to answer but I am in
I realize this may be a very simple question but I need to know
I have a very specific component to realize. I don't really know how to
I realize that this might be a duplicate question but this question is very
I realize this question is rather specific... I'm trying to get my head clearly

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.