1. 4FipS.com
  2. Photos
  3. Videos
  4. Code
  5. Forums
  6. pfQuizzz
  7. About

4FipS.com Forums

Forums dedicated to the projects hosted at 4FipS.com

Skip to content

Polymorphism involving objects with value semantics (C++)

> About coding, hacking, photography, and such...

Polymorphism involving objects with value semantics (C++)

Postby FipS on Tue Dec 17, 2013 10:03 pm

A few months ago, I really enjoyed two very interesting C++ talks (1) (2) given by a very clever guy named Sean Parent. I was immediately enchanted by his approach to polymorphism involving objects with value semantics, which makes virtual dispatch and dynamic memory allocation just an implementation detail rather than a primary means of writing client code. I had been interested in this topic for some time but never encountered such a good source of inspiration, so I've decided to give it a try and come up with this minimal experiment to help me study this approach:

VIEW THE CODE BELOW IN FULL-SCREEN (polymorphism_value_semantics_sample.cpp)
Code: Select all
/*
(c) 2013 +++ Filip Stoklas, aka FipS, http://www.4FipS.com +++
THIS CODE IS FREE - LICENSED UNDER THE MIT LICENSE
ARTICLE URL: http://forums.4fips.com/viewtopic.php?f=3&t=1194
*/

#include <iostream>
#include <vector>
#include <memory>

struct Circle
{
    void draw(std::ostream &out) { out << "Circle::draw()\n"; }
};

struct Square
{
    void draw(std::ostream &out) { out << "Square::draw()\n"; }
};

struct List
{
    template <typename Shape_T>
    void push(Shape_T shape)
    {
        _shapes.emplace_back(If_ptr(new Slot<Shape_T>(std::move(shape))));
    }

    void draw(std::ostream &out)
    {
        for(auto &shape : _shapes)
        {
            shape->draw(out);
        }
    }

 private:

    struct If
    {
        virtual ~If() {}
        virtual void draw(std::ostream &out) = 0;
    };

    template <typename Shape_T>
    struct Slot : public If
    {
        virtual void draw(std::ostream &out) { shape.draw(out); }

        Slot(Shape_T shape) : shape(shape) {}
        Shape_T shape;
    };

    typedef std::unique_ptr<If> If_ptr;
    std::vector<If_ptr> _shapes;
};

int main()
{
    Square s;
    Circle c;
    List l;

    l.push(s);
    l.push(c);
    l.push(c);
    l.push(c);

    l.draw(std::cout);

    return 0;
}

// Compiled under Visual C++ 2012, output:
// Square::draw()
// Circle::draw()
// Circle::draw()
// Circle::draw()

Follow the discussion on Reddit...
User avatar
FipS
Site Admin
 
Posts: 146
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague

Polymorphism involving objects with value semantics (C++)

Sponsor

Sponsor
 

Return to 4FipS.com General Discussion & Blog

Who is online

Users browsing this forum: No registered users and 2 guests