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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:22:32+00:00 2026-05-29T08:22:32+00:00

I have been playing around with various methods of making the Visitor pattern in

  • 0

I have been playing around with various methods of making the Visitor pattern in C++ more dynamic, such that sibling classes don’t have to know about each other, and that allows later extension of the visitor hierarchy. I came up with this example based on “More Effective C++” by Scott Meyers:

class Dummy
{
public:
   void collide(int& gameobject) { }
};

class DynVisitor
{
public:
  template<class Visitor=Dummy, class Arg=int>
  void visit(Arg& target)
  {
    Visitor* vis = dynamic_cast<Visitor*>(this);

    if(vis != nullptr)
    {
      vis->collide(target);
    }
    else
    {
      cerr<<"No implementation!"<<endl;
    }
  }

  virtual ~DynVisitor() { }
};

class GameObject
{
public:
  virtual ~GameObject() { }
  virtual void collide(GameObject& obj) 
  {
    cout<<"Default collide implementation"<<endl;
  }

  virtual void accept(DynVisitor* vis) = 0;
};

class AsteroidVisitor
{
public:
  virtual void collide(Asteroid& target) = 0;
  virtual ~AsteroidVisitor() = 0;
};

class Collider : public DynVisitor, public AsteroidVisitor
{
public:
  virtual void collide(Satellite& target) { cout<<"Satellite collision."<<endl; }
  virtual void collide(Spaceship& target) { cout<<"Spaceship collision."<<endl; }
  virtual void collide(Asteroid& target) { cout<<"Asteroid collision."<<endl; }
  virtual ~Collider() { }
};

class Asteroid : public GameObject
{
public:
  virtual void accept(DynVisitor* visitor)
  {
    visitor->visit<AsteroidVisitor, Asteroid>(*this);
  }
};

int main(int argc, char** argv)
{
  DynVisitor* coll = new Collider();

  GameObject* ast = new Asteroid();

  ast->accept(coll);

  delete ast;

  delete coll;

  return 0;
};

This appears to work as I would expect, printing out “Asteroid collision” when the GameObject passed is an Asteroid, and I can add classes to the hierarchy just by defining a new ABC with a collide() method and extending DynVisitor.

My question is, when I add a new class to the hierarchy, does DynVisitor need to be recompiled?

EDIT: Added the asteroid class… sorry about that.

  • 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-29T08:22:33+00:00Added an answer on May 29, 2026 at 8:22 am

    All objects can collide with each other, so they still need to be visitors of each other and hence there is no added “dynamism”. DynVisitor is a template and thus needs to be in the translation unit and will be recompiled everytime. In fact, in this example, DynVisitor does’t give any benefit because the accept() function can call the collide() function instead of the template visit() function.

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

Sidebar

Related Questions

I have been playing around with the MVP pattern using winforms for the last
I have been playing around with the dynamic abilities of powershell and I was
I have been playing around with creation of pdf documents for a project that
I have been playing around with Dynamic Data but I find the filtering and
I have been playing around with subprocess lately. As I do more and more;
I have been playing around with some debugging and wrote some C code that
I have been playing around with a search control and i have noticed that
I have been playing around with Apache CXF, in particular the various data bindings
I have been playing around with wf4 for a while. I found various articles,
I have been playing around with the twisted framework for about a week now(more

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.