dimanche 2 septembre 2018

apply_visitor does not change object

I have inherited from boost::static_visitor<> and defined a class as follows:

class move_visitor : public boost::static_visitor<> {

private:

    double m_dx, m_dy;

public:

    move_visitor() : m_dx(0.0), m_dy(0.0) {}
    move_visitor(double dx, double dy) : m_dx(dx), m_dy(dy) {}
    ~move_visitor() {}

    void operator () (Point &p) const {
        p.X(p.X() + m_dx);
        p.Y(p.Y() + m_dy);
    }

    void operator () (Circle &c) const {

        Point center_point(c.CentrePoint().X() + m_dx, c.CentrePoint().Y() + m_dy);
        c.CentrePoint(center_point);
    }

    void operator() (Line &l) const {
        Point start(l.Start().X() + m_dx, l.Start().Y() + m_dy),
            end(l.End().X() + m_dx, l.End().Y() + m_dy);

        l.Start(start);
        l.End(end);

    }
};

This class is supposed to change the x and y position of some objects, Point, Line and Circle.

When I execute the following code:

int main()
{

typedef boost::variant<Point, Line, Circle> ShapeType;

Point p(1, 2);
Line l(p, p);
Circle c(p, 5);

ShapeType shape_variant;

std::cout << p << "\n\n"
          << l << "\n\n" 
          << c << "\n\n";

shape_variant = p;
boost::apply_visitor(move_visitor(2, 2), shape_variant);

std::cout << p << std::endl;

shape_variant = l;
boost::apply_visitor(move_visitor(2, 2), shape_variant);
std::cout << std::endl << l << std::endl;

return 0;
}

p remains as 1, 2 and so does l. Why aren't my object changing after 'apply_visitor`?

Aucun commentaire:

Enregistrer un commentaire