Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concept Based Polymorphism

I have been reading up on Concept based Inheritance in C++. I have a attached a code sample for all. I am basically asking if this is a correct implementation of the concept of this? I am new to this so I am just putting down what is in my mind. Any comments / critisims are welcome.

#include "stdafx.h"
#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

struct Point{
    int x;
    int y;
};

class graphics_surface{

    class drawable_concept{
    public:
        virtual void draw(Point const& coordinate) {};
        virtual ~drawable_concept() {};
    };

    template<class T>
    class drawable_model : public drawable_concept{
    public:
        drawable_model(T& item) : item_(item){}
        void draw(Point const& coordinate){
            item_.draw(coordinate);
        }
        ~drawable_model(){}
    private:
        T item_;
    };

public:

    template<class T>
    void push_back(T& drawable){
        v_.push_back(shared_ptr<drawable_concept>( new drawable_model<T>(drawable)));
    }

    void draw(Point const& coordinate) {
        for_each(v_.begin(), v_.end(), [&](shared_ptr<drawable_concept>& concept){
            concept->draw(coordinate);
        });
    }

private:
    vector<shared_ptr<drawable_concept>> v_;
};

struct triangle{
    void draw(Point const& p){
        cout << "Triangle: " << p.x << "," << p.y << endl;
    }
};

struct square{
    void draw(Point const& p){
        cout << "Sqaure: " << p.x << "," << p.y << endl;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{

    Point p;
    p.x = 1;
    p.y = 2;

    graphics_surface surface;
    surface.push_back(triangle());

    surface.draw(p);

    return 0;
}

Thanks in advance.

Blair

like image 369
Blair Davidson Avatar asked Feb 17 '14 06:02

Blair Davidson


People also ask

What is polymorphism C++?

Polymorphism in C++ means, the same entity (function or object) behaves differently in different scenarios. Consider this example: The “ +” operator in c++ can perform two specific functions at two different scenarios i.e when the “+” operator is used in numbers, it performs addition.

Why do we need polymorphism?

Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.


2 Answers

A few points:

  • I don't see any good reason to put drawable_concept or drawable_model inside graphics_surface - you just prevent reuse of something that's potentially useful in other container types...

  • you have some const issues

    • draw should probably be const (and function definitions should not be followed by semicolons ;-)

    • drawable_model(T& item) should take item by const reference

    • push_back(T& drawable) shoudl take drawable by const reference

  • you should use make_shared for exception safety

  • the "factory" functionality would arguably be better off separated into a separate function, rather than buried inside push_back

like image 151
Tony Delroy Avatar answered Sep 19 '22 13:09

Tony Delroy


Your approach here is more about type erasure than it is about Concept based programming. It's an extension of the idea used by boost::any. Concepts are a set of constraints on a type required by a class or function template. The STL has concepts such as ForwardIterator and InputIterator. These are constraints that are expected to be true for parameters passed to some std algorithms for example.

like image 22
Peter R Avatar answered Sep 19 '22 13:09

Peter R