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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:51:08+00:00 2026-05-22T14:51:08+00:00

Let’s say I have the following code: struct Z; struct A { virtual void

  • 0

Let’s say I have the following code:

struct Z;

struct A
{
  virtual void Do (Z & z) const;
};

struct B : public A {};

struct Z
{
  void use (A const & a) {}
  void use (B const & b) {}
};


void A::Do(Z& z) const{
  z.use(*this);
}

Right now, when I call B.do, the type of this is A, which make sense, because the implementation of do is defined in A.

Is there any way to have calls to B.do use use (B const &) without having to copy-paste the same code for do from A into B? In my actual code I have about 15 (and growing) classes derived from some base class and it seems a waste having to copy-paste the identical code for do everytime.

[Edit] Clarification: all Do does is call use, nothing else. Do and use are the accept & visit functions from the Visitor pattern.

  • 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-22T14:51:09+00:00Added an answer on May 22, 2026 at 2:51 pm

    Since you now clarified that what you want is the visitor pattern, well, sorry, but that’s just how it is. This answer shows how the visitor pattern with double dispatch works.


    I thought of a nice way using CRTP, but this may or may not work for you, depending on the circumstances.
    (Note: I used the code from the linked answer, so the names don’t match, but I hope you get the idea.)

    // your Z
    class Visitor;
    
    // superclass needed for generic handling
    struct Superbase{
      virtual void Accept(Visitor& v) = 0;
    };
    
    // your A
    template<class Der>
    class Base : public Superbase{
    public:
        void Accept(Visitor& v){
            v.Visit(static_cast<Der&>(*this));
        }
    };
    
    // your B
    class Derived1 : public Base<Derived1> {
    };
    
    // new C
    class Derived2 : public Base<Derived1> {
    };
    
    class Visitor {
    public:
        virtual void Visit(Superbase& sup){
          // generic handling of any Superbase-derived type
        }
    
        virtual void Visit(Derived1& d1){
          // handle Derived1
        }
    
        virtual void Visit(Derived2& d2){
          // handle Derived1
        }
    };
    
    int main(){
        Visitor v;
        Derived1 d1;
        d1.Accept(v);
    }
    

    The only problem: Now you’re missing the chance to have a generic handle to any type of A, since functions can’t be both virtual and templates. 😐
    Scrape that, found a solution using a Superbase base class. 🙂 This even allows you to have a container of Superbases and take full advantage of polymorphism. 🙂

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

Sidebar

Related Questions

Let's say I have this: SolutionSet(const SolutionSet &solutionSet) { this->capacity_ = solutionSet.capacity_; this->solutionsList_ =
Let's consider the following 3 code lines: struct stat buffer; status = lstat(file.c_str(), &buffer);
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have the following within my source code, and I want to
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say that I have classes like this: public class Parent { public int
Let's say i have this block of code, <div id=id1> This is some text
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have saved this kind of data (some text '.date(d).' some text)
Let's say I have this: float i = 1.5 in binary, this float is

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.