shane
shane

Reputation: 1863

Visitor pattern from base class

I am trying to implement the visitor pattern in c++ https://en.wikipedia.org/wiki/Visitor_pattern

How can I use this pattern when dealing with objects as their base class? I want the visitor to be able to parse the class hierarchy. In my specific case, not the example I give, I want to parse an array of objects into a json list where each object has both the parent class members and the specific subclass members. Is the visitor pattern not the correct approach here?

#include <iostream>
#include <vector>

class Animal;
class Dog;
class Cat;

class Visitor {
public:
    virtual void visit(Animal *a) = 0;
    virtual void visit(Dog *d) = 0;
    virtual void visit(Cat *c) = 0;
};

struct Animal {
    void accept(Visitor &v) { v.visit(this); }
    int a;
protected:
     Animal(int an) : a(an) {}
};

struct Dog : Animal {
    Dog(int dn, int an) : Animal(an), d(dn) {}
    void accept(Visitor &v) { v.visit(this); }
    int d;
};

struct Cat : Animal {
    Cat(int cn, int an) : Animal(an), c(cn) {}
    void accept(Visitor &v) { v.visit(this); }
    int c;
};

struct ConcreteVisitor : Visitor {
    void visit(Animal *a) { 
        std::cout << "Animal<" << a->a << ">"; 
    }

    void visit(Dog *d) {
        std::cout << "Dog<" << d->d << ", "; 
        visit((Animal *)d); 
        std::cout << ">" << std::endl;
    }

    void visit(Cat *c) {
        std::cout << "Cat<" << c->c << ", "; 
        visit((Animal *)c); 
        std::cout << ">" << std::endl;
    }
};

int main() {
    std::vector<Animal *> animals;
    animals.push_back(new Dog(4, 5));
    animals.push_back(new Cat(6, 7));

    ConcreteVisitor v;
    for (Animal *a : animals) {
         a->accept(v);
     }
}

This prints Animal<5> Animal<7>

Upvotes: 2

Views: 927

Answers (1)

DAle
DAle

Reputation: 9127

You should make accept virtual as well.

Upvotes: 3

Related Questions