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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:44:48+00:00 2026-05-23T01:44:48+00:00

I want to expand a declared Visitor through inheritance and have the run-time environment

  • 0

I want to expand a declared Visitor through inheritance and have the run-time environment search the descendents of the Visitor for the correct method to execute. I can have this in C#, but I’m looking to use it in C++. I tried the following code in g++ and the descendent method is not called; only the method for the base class is called.

#include <iostream>
using namespace std;

struct Field; // Forward declaration

struct Visitor
{
    virtual void visit(Field& f) = 0; // Visits Field objecs and objects derived from Field.
};

struct Field_String;

struct Visitor_Field_String : public Visitor
{
    // Extend the Visitor by specifying a visitation
    //     for Field_String
    virtual void visit(Field_String& fs) = 0;
};

struct Field
{
    void accept_visitor(Visitor& v)
    {
        cout << "Field accepting visitor.\n";
        v.visit(*this);
    }
};

struct Field_String : public Field
{
    void accept_visitor(Visitor& v)
    {
        cout << "Field_String accepting visitor.\n";
        v.visit(*this);  // Line 1
    }
};

struct Full_Visitor : Visitor_Field_String
{
    void visit(Field& f)
    {
        cout << "Visiting a Field object\n";
        return;
    }
    void visit(Field_String& fs)
    {
        cout << " Visiting a Field_String object\n";
        return;
    }
};


int main(void)
{
    Field_String fs;
    Full_Visitor visitor;
    fs.accept_visitor(visitor);
    return 0;
}

I get the following output:

# g++ --version
g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# g++ -o virtual_visitor.exe virtual_visitor.cpp
# ./virtual_visitor.exe
Field_String accepting visitor.
Visiting a Field object

The output I want is:

Field_String accepting visitor.
Visiting a Field_String object

My two questions:

  1. Why is the visit method in the
    descendent Visitor not executed?
  2. How do I execute the visit method
    in the descendent Visitor using
    polymorphism?

Note: The objective is to reduce the classes specified in a Visitor class, by using inheritance and allowing for the cases that may not use all of the classes specified in the Visitor.

Note: This is not double dispatch, but extended dispatch.

  • 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-23T01:44:49+00:00Added an answer on May 23, 2026 at 1:44 am

    You cannot do that in C++ (can you really do it in C# without reflection?) On the particular questions:

    1. The compiler resolves the function overload to use based on the static type of the reference, and the final overrider of that function based on the dynamic type of the object.

    2. You need to provide all of the different overloads in the base class. If you cannot do it, you can do nasty things like dynamic_cast‘s to try and determine whether the Visitor received has support for that particular field, but I would avoid it at all costs.
      dispatch*.

    Since the different field types are not being used polymorphically (or at least it does not seem like it, since the accept_visitor functions are not virtual), why don’t you accept the concrete visitor type?

    struct Field_String : Field
    {
        void accept_visitor(Visitor_Field_String& v)
        {
            cout << "Field_String accepting visitor.\n";
            v.visit(*this);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a button which I want to expand/contract. I found a way
I want to expand hidden text on a webpage and after some (re)search on
I just want to expand this following method into something more generic, which should
I have a git repository in a directory a/b/, and I want to expand
I have a image of a shelf that I want to expand and contract
If I have a Narray with the shape 100, 10000 and want to expand
I have an R data frame containing a factor that I want to expand
I want to expand a method to the String class in Sinatra, in the
I have UITextView and I want to expand it dynamically. If the text is
I want to expand UITableView section where I have to show UITableViewCells . What

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.