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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:59:29+00:00 2026-05-25T11:59:29+00:00

Imagine I have abstract base class Shape , with derived classes Circle and Rectangle

  • 0

Imagine I have abstract base class Shape, with derived classes Circle and Rectangle.

class Shape {};
class Circle : public Shape {};
class Rectangle : public Shape {};

I need to determine if two shapes are equal, assuming I have two Shape* pointers. (This is because I have two instances of vector<Shape*> and I want to see if they have the same shapes.)

The recommended way to do this is double dispatch. What I’ve come up with is this (greatly simplified here, so that shapes are equal to all other shapes of the same type):

class Shape {
public:
    virtual bool equals(Shape* other_shape) = 0;
protected:
    virtual bool is_equal(Circle& circle) { return false; };
    virtual bool is_equal(Rectangle& rect) { return false; };
    friend class Circle;    // so Rectangle::equals can access Circle::is_equal
    friend class Rectangle; // and vice versa
};

class Circle : public Shape {
public:
    virtual bool equals(Shape* other_shape) { return other_shape->is_equal(*this); };
protected:
    virtual bool is_equal(Circle& circle) { return true; };
};

class Rectangle : public Shape {
public:
    virtual bool equals(Shape* other_shape) { return other_shape->is_equal(*this); };
protected:
    virtual bool is_equal(Rectangle& circle) { return true; };
};

This works, but I have to add a separate equals function and friend declaration in Shape for each derived class. Then I have to copy-paste the exact same equals function into each derived class, too. This is an awful lot of boilerplate for say, 10 different shapes!

Is there a simpler way to do it?

dynamic_cast is out of the question; too slow. (Yes, I benchmarked it. Speed matters in my app.)

I tried this but it doesn’t work:

class Shape {
public:
    virtual bool equals(Shape* other_shape) = 0;
private:
    virtual bool is_equal(Shape& circle) { return false; };
};

class Circle : public Shape {
public:
    virtual bool equals(Shape* other_shape) { return other_shape->is_equal(*this); };
private:
    virtual bool is_equal(Circle& circle) { return true; };
};

class Rectangle : public Shape {
public:
    virtual bool equals(Shape* other_shape) { return other_shape->is_equal(*this); };
private:
    virtual bool is_equal(Rectangle& circle) { return true; };
};

equals() always returns false, even on identical shapes. It seems dispatch is always choosing the is_equal(Shape&) base function, even when a “more specific” match is available. This probably makes sense but I don’t understand C++ dispatch well enough to know why.

  • 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-25T11:59:30+00:00Added an answer on May 25, 2026 at 11:59 am

    When you create methods like this:

    virtual bool is_equal(Shape& circle) { return false; };
    

    And in the subclass,

    virtual bool is_equal(Circle& circle) { return true; };
    

    These are not the same method. You have two separate virtual methods, neither of which is overridden (they are overloaded not even overloaded, as Ben Voigt pointed out). When you call Shape::is_equal, there is only one version: Shape::is_equal(Shape&)… which is not overridden and always returns false.

    You would have to define the separate overloaded methods in the parent class and then override them in the child class. For example,

    class Shape {
        // Choice between these two methods happens at compile time...
        virtual bool is_equal(Circle& circle) { return false; };
        virtual bool is_equal(Rectangle& circle) { return false; };
    };
    
    class Rectangle : Shape {
        // Choice between this and Shape::is_equal(Rectangle&) happens at runtime...
        virtual bool is_equal(Rectangle& circle) { return true; };
    };
    

    However, using tricks like this, you will probably not approach the performance or simplicity of the way a C programmer would do it:

    typedef enum {
        SHAPE_CIRCLE,
        SHAPE_RECTANGLE
    } shape_type_t;
    
    struct shape {
        shape_type_t type;
    };
    
    struct circle {
        shape_type_t type;
        ...
    };
    
    struct rectangle {
        shape_type_t type;
        ...
    };
    
    bool shape_equal(struct shape *x, struct shape *y)
    {
        if (x->type != y->type)
            return false;
        switch (x->type) {
        case SHAPE_CIRCLE:
            return circle_equal((struct circle *) x, (struct circle *) y);
        case SHAPE_RECTANGLE:
            ...;
        }
    }
    

    If overloading and virtual methods are making your code more complicated than the C version, then you may wish to rethink whether you solve this particular problem with overloading and virtual methods.

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

Sidebar

Related Questions

Imagine that I have a generic base class like this: public abstract class AnimalDTO<TA,
In C#, I have a class hierarchy with a couple of abstract base classes
Imagine we have following classes: public interface MyInterface<T> { List<T> getList(T t); } abstract
We have a set of about two dozen classes that inherit from a base
I have python class trees, each made up of an abstract base class and
Let's say in some abstract ViewModel base-class I have a plain-old property as follows:
Imagine we have a class public class MyClass { private string _val; public MyClass(string
Imagine I have a property defined in global.asax. public List<string> Roles { get {
In my model I have an abstract User class, and multiple subclasses such as
Imagine that I have a Debtor class. With Hibernate, I will define the class

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.