mardi 26 novembre 2019

Implement hash for custom class C++

I have a circle defined like so:


class Circle {
public:
    int x;
    int y;
    int r;

    Circle() : x(0), y(0), r(0) {}

    Circle(int x, int y, int r) {
        this->x = x;
        this->y = y;
        this->r = r;
    }

    double area() {
        return PI * pow(r, 2);
    }
};

I want to be able to add it to a set, based on the hash of its center (x and y)

What's the correct and idiomatic way of doing this in C++ 11?

Aucun commentaire:

Enregistrer un commentaire