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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:09:39+00:00 2026-06-07T06:09:39+00:00

Consider the following hierarchy: class Base { virtual void Method() = 0; virtual void

  • 0

Consider the following hierarchy:

class Base
{
   virtual void Method() = 0;
   virtual void Accept(Visitor *iVisitor) = 0;
};
class Derived1: public Base
{
   virtual void Method(){//impl}
   virtual void Accept(Visitor *iVisitor)
   {
        iVisitor->Visit(this);
   }
};
class Derived2: public Base
{
   virtual void Method(){//impl}
   virtual void Accept(Visitor *iVisitor)
   {
       iVisitor->Visit(this);
   }
};

and the visitor class:

class VisitorInterface
{
    virtual void Visit(Derived1 * param);
    virtual void Visit(Derived2 * param);
}
class Visitor: public VisitorInterface
{
    void Visit(Derived1 * param){}
    void Visit(Derived2 * param){}
}

Usually I use the visitor pattern to achieve double dispatching when the overload method depends on the parameter type, but I have only the pointer to the base class.

For example:

void foo(Visitor *visitorPtr, Base * basePtr)
{
    basePtr->Accept(visitorPtr);    
} 

I think this is the only way to achieve double dispatching since the dynamic binding of virtual functions should happen only on the object upon which the method is called and not on its parameters (derived types).

Now I encountered a new situation, where I need a sort of Visit method overloading on multiple parameters. Something like this:

class VisitorInterfaceMultiple
{
    virtual void Visit(Derived1 * param1, Derived2 * param2);
    virtual void Visit(Derived2 * param1, Derived3 *param2);
}

I cannot use the classical visitor pattern solution because accept method is called on only one of the parameters.

My question is: does exist any similar visitor pattern solution, or something similar, that I could use in this situation? (I need to overload Visit with exact 2 parameters, no more than 2).

  • 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-07T06:09:40+00:00Added an answer on June 7, 2026 at 6:09 am

    I created for you “triple” dispatch pattern: http://ideone.com/FoXNW
    It is quite easy.
    Main part below:

    class Derived1;
    class Derived2;
    class Visitor;
    class Base
    {
    public:
       virtual void Accept(Visitor &iVisitor, Base& param1) = 0;
       virtual void Accept(Visitor &iVisitor, Derived1& param2) = 0;
       virtual void Accept(Visitor &iVisitor, Derived2& param2) = 0;
    };
    
    class Visitor
    {
    public:
        virtual void Visit(Derived1 & param1, Derived1 &param2) { cout << "11\n"; }
        virtual void Visit(Derived1 & param1, Derived2 &param2) { cout << "12\n"; }
        virtual void Visit(Derived2 & param1, Derived1 &param2) { cout << "21\n"; }
        virtual void Visit(Derived2 & param1, Derived2 &param2) { cout << "22\n"; }
    };
    
    class Derived1: public Base
    {
    public:
       virtual void Accept(Visitor &iVisitor, Base& param1) 
       { param1.Accept(iVisitor, *this); }
       virtual void Accept(Visitor &iVisitor, Derived1& param2)
       { iVisitor.Visit(*this, param2); }
       virtual void Accept(Visitor &iVisitor, Derived2& param2)
       { iVisitor.Visit(*this, param2); }
    };
    class Derived2: public Base
    {
    public:
       virtual void Accept(Visitor &iVisitor, Base& param1) 
       { param1.Accept(iVisitor, *this); }
       virtual void Accept(Visitor &iVisitor, Derived1& param2)
       { iVisitor.Visit(*this, param2); }
       virtual void Accept(Visitor &iVisitor, Derived2& param2)
       { iVisitor.Visit(*this, param2); }
    };
    
    void Visit(Visitor& visitor, Base& param1, Base& param2)
    {
       param2.Accept(visitor, param1);
    }
    

    Note that implementation of Derived1 and Derived2 is literally identical. You can enclose this in macro if you have more derived.

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

Sidebar

Related Questions

Consider the following class hierarchy: base class Object with a virtual method foo() an
Consider the following class hierarchy: public abstract class Entity { public virtual int Id
Consider the following class hierarchy: public class Foo { public string Name { get;
Let's consider the following simplified Resource hierarchy: public abstract class Resource { static public
Consider the following simplified interface inheritence hierarchy: // Starting point: public interface Base {
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following simple example: [DataContract({0}Base)] public class Base<T> where T : Entity<T> {
Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);
Consider following 2 programs giving same error First calss: public class Testing { Testing
consider following: 1st APPROACH: public void f3() { f2(); f1(); } and this ...

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.