Skip to Content
Hands-On Design Patterns with C++
book

Hands-On Design Patterns with C++

by Fedor G. Pikus
January 2019
Intermediate to advanced
512 pages
14h 5m
English
Packt Publishing
Content preview from Hands-On Design Patterns with C++

Basic Visitor in C++

The only way to really understand how the Visitor pattern operates is to work through an example. Let's start with a very simple one. First, we need a class hierarchy:

class Pet {    public:    virtual ~Pet() {}    Pet(const std::string& color) : color_(color) {}    const std::string& color() const { return color_; }    private:    std::string color_;};class Cat : public Pet {    public:    Cat(const std::string& color) : Pet(color) {}};class Dog : public Pet {    public:    Dog(const std::string& color) : Pet(color) {}};

In this hierarchy, we have the Pet base class and several derived classes for different pet animals. Now we want to add some operations to our classes, such as feed the pet or play with the pet. The implementation depends on the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Structural Design Patterns in Modern C++

Structural Design Patterns in Modern C++

Umar Lone

Publisher Resources

ISBN: 9781788832564Supplemental Content