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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:06:43+00:00 2026-05-27T23:06:43+00:00

I am looking into using the Visitor pattern. Some of the examples I see

  • 0

I am looking into using the Visitor pattern. Some of the examples I see suggest using an accept(Visitor) function in every Element subclass. Is the idea of that function just so that a visitor can visit a collection that contains polymorphic types? In this code, I use a visitor to do two types of accumulation, and it doesn’t require accept().

#include <iostream>
#include <vector>

class IntItem
{
public:
  IntItem(const int a) : IntData(a){}

  int IntData;
};

class DoubleItem
{
public:
  DoubleItem(const double a) : DoubleData(a){}

  double DoubleData;
};

class Visitor
{
public:
  virtual void Visit(IntItem &item) = 0;
  virtual void Visit(DoubleItem &item) = 0;
};


class SumAccumulator : public Visitor
{
public:

  SumAccumulator() : Sum(0) {}
  void Visit(IntItem &item)
  {
    Sum += item.IntData;
  }

  void Visit(DoubleItem &item)
  {
    Sum += item.DoubleData;
  }

  double Sum;
};

class AverageAccumulator : public Visitor
{
public:

  AverageAccumulator() : Average(0), Counter(0) {}
  void Visit(IntItem &item)
  {
    Counter++;
    Average = (Counter - 1) * Average + item.IntData;
    Average /= Counter;
  }

  void Visit(DoubleItem &item)
  {
    Counter++;
    Average = (Counter - 1) * Average + item.DoubleData;
    Average /= Counter;
  }

  int Counter;
  double Average;
};

class IntCollection
{
public:
  void Visit(Visitor &visitor)
  {
    for(unsigned int i = 0; i < IntItems.size(); ++i)
      {
      visitor.Visit(IntItems[i]);
      }
  }

  void AddIntItem(const IntItem& item)
  {
    IntItems.push_back(item);
  }

private:
  std::vector<IntItem> IntItems;

};

class DoubleCollection
{
public:
  void Visit(Visitor &visitor)
  {
    for(unsigned int i = 0; i < DoubleItems.size(); ++i)
      {
      visitor.Visit(DoubleItems[i]);
      }
  }

  void AddDoubleItem(const DoubleItem& item)
  {
    DoubleItems.push_back(item);
  }

private:
  std::vector<DoubleItem> DoubleItems;
};

int main(int argc, char *argv[])
{
  /////// Ints ////////
  IntCollection intCollection;
  for(unsigned int i = 0; i < 4; ++i)
    {
    intCollection.AddIntItem(IntItem(i));
    }

  SumAccumulator intSumAccumulator;
  intCollection.Visit(intSumAccumulator);
  std::cout << "int sum: " << intSumAccumulator.Sum << std::endl;

  AverageAccumulator intAverageAccumulator;
  intCollection.Visit(intAverageAccumulator);
  std::cout << "int average: " << intAverageAccumulator.Average << std::endl;

  /////// Doubles ////////
  DoubleCollection doubleCollection;
  for(unsigned int i = 0; i < 4; ++i)
    {
    doubleCollection.AddDoubleItem(DoubleItem(static_cast<double>(i) + .1));
    }
  SumAccumulator doubleSumAccumulator;
  doubleCollection.Visit(doubleSumAccumulator);
  std::cout << "double sum: " << doubleSumAccumulator.Sum << std::endl;

  AverageAccumulator doubleAverageAccumulator;
  doubleCollection.Visit(doubleAverageAccumulator);
  std::cout << "double average: " << doubleAverageAccumulator.Average << std::endl;

  return 0;
}

In this code, I do use accept(), and the only difference is that the container can contain objects of varying types in the same container:

#include <iostream>
#include <string>
#include <vector>

class IntElement;
class DoubleElement;

class Visitor
{
public:
  virtual void visit(IntElement *e) = 0;
  virtual void visit(DoubleElement *e) = 0;
};

class Element
{
public:
  virtual void accept(class Visitor &v) = 0;
};

class IntElement: public Element
{
public:
  IntElement(int i) : IntData(i){}
  /*virtual*/void accept(Visitor &v)
  {
    v.visit(this);
  }

  int IntData;
};

class DoubleElement: public Element
{
public:
  DoubleElement(double d) : DoubleData(d){}
  /*virtual*/void accept(Visitor &v)
  {
    v.visit(this);
  }

  double DoubleData;
};

class SumVisitor: public Visitor
{
public:
  SumVisitor() : Sum(0){}
  /*virtual*/void visit(IntElement *e)
  {
    Sum += e->IntData;
  }
  /*virtual*/void visit(DoubleElement *e)
  {
    Sum += e->DoubleData;
  }

  double Sum;
};

class AverageVisitor: public Visitor
{
public:
  AverageVisitor() : Counter(0) , Average(0){}
  /*virtual*/void visit(IntElement *e)
  {
    Counter++;
    Average = (Counter - 1) * Average + e->IntData;
    Average /= Counter;
  }
  /*virtual*/void visit(DoubleElement *e)
  {
    Counter++;
    Average = (Counter - 1) * Average + e->DoubleData;
    Average /= Counter;
  }
  double Average;
  int Counter;
};

int main()
{
  std::vector<Element*> elements;
  elements.push_back(new IntElement(0));
  elements.push_back(new IntElement(1));
  elements.push_back(new DoubleElement(2));
  elements.push_back(new DoubleElement(3));

  SumVisitor sumVisitor;
  AverageVisitor averageVisitor;
  for (int i = 0; i < elements.size(); i++)
    {
    elements[i]->accept(sumVisitor);
    elements[i]->accept(averageVisitor);
    }
  std::cout << "sum: " << sumVisitor.Sum << std::endl;
  std::cout << "average: " << averageVisitor.Average << std::endl;
}

Is this distinction correct? That is, if I only plan on having homogeneous containers I do not need to implement accept functions?

  • 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-27T23:06:44+00:00Added an answer on May 27, 2026 at 11:06 pm

    Is this distinction correct? That is, if I only plan on having
    homogeneous containers I do not need to implement accept functions?

    Yes, that’s the essence of the pattern.

    Basically, if you have a relatively stable Element hierarchy, it allows you to add new Visitor derivatives as necessary. The visitors can then be invoked without the invoker knowing the concrete type of the element being operated on.

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

Sidebar

Related Questions

I was looking into using some .NET code from within a Delphi program, I
We are looking into using an ORM and I wanted some opinions/comparisons The basic
I'm looking into using ftplib (and possibly ftputil ) for doing some automated FTP
Client looking into using QR codes in print advertising that will reward the visitor
I am looking into using a producer\consumer threading pattern to run through a list
I'm looking into using Eclipse RCP on a new application where some widgets will
I'm looking into using Visual Studio 2008's built in unit test projects instead of
I'm looking into using Lucene and/or Solr to provide search in an RDBMS-powered web
I am looking into using only a ddl to run my query, not a
I've been looking into using the UIPageControl for a scrolling part of an application,

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.