I have referred some class diagram to actually create visitor.#include <iostream>using namespace std; //know should not be used, but for simplicityclass Book;class Pen;class Computer;class ICartVisitor{public:    virtual int Visit(Book &) = 0;    virtual int Visit(Pen &) = 0;    virtual int Visit(Computer &) = 0;};class IElement{public:    virtual int accept (ICartVisitor& cartVisitor) = 0;    virtual int getPrice () = 0;};class Book : public IElement{public:    int accept (ICartVisitor& cartVisitor)    {        return cartVisitor.Visit(*this);    }    int getPrice ()    {        return 100;    }};class Pen : public IElement{public:    int accept (ICartVisitor& cartVisitor)    {        return cartVisitor.Visit(*this);    }    int getPrice ()    {        return 5;    }};class Computer : public IElement{public:    int accept (ICartVisitor& cartVisitor)    {        return cartVisitor.Visit(*this);    }    int getPrice ()    {        return 10000;    }};class CartVisitor : public ICartVisitor{public:    //inlining all the functions for simplicity    int Visit(Book & book)     {        return book.getPrice();    }    int Visit(Pen & pen)     {        return pen.getPrice();    }    int Visit(Computer & computer)     {        return computer.getPrice();    }};int main(int argc, char* argv[]){    int total = 0;    ICartVisitor *cartVisitor = new CartVisitor();    total += cartVisitor->Visit(*(new Computer()));    total += cartVisitor->Visit(*(new Book()));    total += cartVisitor->Visit(*(new Pen()));    cout<<total;    return 0;}
Visitor Pattern in C++ 11
c++;object oriented;c++11;design patterns
Stop using new    // No No No    ICartVisitor *cartVisitor = new CartVisitor();Just create a local object:    ICartVisitor  cartVisitor; Again. You don't need to dynamically create objects.    total += cartVisitor->Visit(*(new Computer()));    // In this case a temporary object will work.    total += cartVisitor->Visit(Computer());  // Creates a temporary object                                              // That is correctly destroyed.Visitor patternThe visitor pattern is good when your class hierarchy is stable and does not change much (or at all).Also the interface that indicates you accept a visitor usually only has the accept() method. Seems like you are mixing two interfaces into IElement.class IElement{public:    virtual int accept (ICartVisitor& cartVisitor) = 0;    virtual int getPrice () = 0;};I don't see anything wrong with your visitor pattern. But I would not expect it to return an int (it can just a bit unexpected). This is because usually when you visit an object it means that object will have sub-objects that need to be visited. Rather than returning state via the calls to visit() you mutate the state of the visitor object. When you are finished visting all the object just query the visitor about its state.class ICartVisitor{public:    virtual void Visit(ShoopingCart& cart) = 0;    virtual void Visit(Book &)             = 0;    virtual void Visit(Pen &)              = 0;    virtual void Visit(Computer &)         = 0;};Then an implementation would look like this:class CartVisitor : public ICartVisitor{    int costOfItemsInCart;public:    CartVisitor()       : costOfItemsInCart(0)    {}    int getCostOfCart() const {return costOfItemsInCart;}    void Visit(ShoopingCart & cart)     {        // Nothing happens you can't buy a cart.        // Note it is the accept on the shopping cart that        // calls the visit on all the items in the cart.        // The visitor does not need to know anything about        // the carts implementation. It just knows that each        // object knows about its own sub objects and will be        // passed to them via the accept method.    }    void Visit(Book & book)     {        //  Book does not need a virtual `getPrince()` method        //  Being the visitor of Book you know that it is a book        //  and its whole interface. You don't need to implement        //  a limited interface (you have the whole Book interface).        //  You just need to have a book with `getPrice()` method on it.        //  for this particular implementation of the Visitor.        //  Other visitors may not need the price (they may be calculating        //  the weight of the cart for determining shipping costs).        costOfItemsInCart += book.getPrice();    }    void Visit(Pen & pen)     {        costOfItemsInCart += pen.getPrice();    }    void Visit(Computer & computer)     {        costOfItemsInCart += computer.getPrice();    }};