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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:00:36+00:00 2026-06-05T23:00:36+00:00

I have an abstract type A , and two derived types A1 and A2

  • 0

I have an abstract type A, and two derived types A1 and A2.
I want to add a method M to class A, which takes a parameter of type A. But, I need ad hoc polymorphism.

Indeed, I need 3 implementations : A1::M(A1 a), A1::M(A2 a) and A2::(A1 a), A2::M(A2 a).
But I want an abstract way to call the method M with pointers of type A.

I could put all signatures declaration in class A, but it sucks.

  • 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-05T23:00:37+00:00Added an answer on June 5, 2026 at 11:00 pm

    This is double dispatch. When you write:

    A* p1;
    A* p2;
    p1->M(*p2);
    

    should dispatch both on the type of *p1 and the type of *p2.

    Before starting, you must realize that this means n^2 functions for
    n different derived types. And that somewhere, someone must be aware
    of all of the derived types (unless you can define some sort of
    “default” implemenation for an unknown pair of types).

    There are two ways of implementing this. The simplest, if the hierarchy
    is closed (i.e. client code cannot introduce new derived classes) does
    use a host of virtual functions in the base class—generally
    protected, since they’re not designed to be called outside the
    hierarchy:

    //  Forward references needed for all derived classes...
    class A1;
    class A2;
    //  ...
    
    class A
    {
    protectd:
        virtual void doM(A1* arg) = 0;
        virtual void doM(A2* arg) = 0;
        //  ...
    
    public:
        virtual void M(A& arg) = 0;
    };
    

    In the derived classes, the implementation of M is always the same:

    void A1::M(A& arg)
    {
        arg.doM( this );
    }
    

    This is simple, and relatively efficient, but requires changes in the
    abstract base and all of the derived classes (which have to implement
    the new virtual function) each time you add a new derived class. It’s
    useful for closed hierarchies, however; I’ve used it in classes using
    the strategy pattern for part of their behavior, where the various
    strategies were all defined in the source file, and not exposed to the
    clients (and the abstract base of the strategies was only forward
    declared in the header, so no header changes were necessary if I added a
    strategy).

    A more general solution would involve an std::map, with a pair of
    typeid as index. You can’t use typeid directly, since it’s not
    copyable. C++11 provides a type_index to wrap it; if you’re using an
    older compiler, it’s fairly trivial to implement one yourself. The
    basic principle is something along the lines of (probably in A itself):

    typedef std::pair<std::type_index, std::type_index> TypePairKey;
    typedef void (*FuncPtr)( M* arg1, M* arg2 );
    typedef std::unordered_map<TypePairKey, FuncPtr> DispatchMap;
    
    static DispatchMap ourDispatchMap;
    

    with:

    void M( A& arg )        //  NOT virtual !!!
    {
        DispatchMap::iterator entry
            = ourDispatchMap.find(
                    DispatchMap::value_type( typeid( *this ), typeid( arg ) ) );
        assert( entry != ourDispatchMap.end() );
            //  Or some default handling, maybe throw NotYetImplemented()
        (*entry->second)( this, &arg );
    }
    

    The real problem comes with writing each of the individual functions and
    inserting their addresses in the map (before first use). The functions
    themselves, of course, can use dynamic_cast, or even static_cast, if
    you can be sure that they will only be called from here, and they can be
    friend(s) of the class(es) involved, but there are still
    n2 of them. (One frequent solution is to make them
    static members of one of the classes, and to have each derived class
    define a static member of a type which does the registration of all of
    the functions its responsible for.)

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

Sidebar

Related Questions

I have an abstract test class that has two type parameters. I need to
I want to have an abstract class Server with an abstract method called Initialize
I have an abstract class MainClass which composes Animal class. I derive two classes
I have a class, Container<T>, which has a ContainerContents<T>. The Container actually takes two
In c++ I want to have an array of the abstract type Query which
Basically, I have: public abstract class AbstractClass { public AbstractClass( Type arg0, Type arg1,
I have a class like this: abstract class CrudResource extends Controller { type ResourceIdType
I have a form builder class which inherits from AbstractType and I need to
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
A simple question... I have an abstract class Cell and two classes BorderCell and

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.