jeudi 2 juillet 2020

Inheritance and friend functions, accessing protected members from base class

Let say that I have a big class Circle with a lot of members and functions. To proceed a large amount of data I decided to create class PotentialCirlce (with only 3 members - x, y, r), do most of preprocessing based on PotentialCirlce and in the last stage create objects Circle.

a) is it correct approach? do It influence on performance or rather should I use only Circle.

It seems to me that I can use inheritance:

class potentialCircle {
protected:
    point_t center;
    unsigned int radius;
public:
    potentialCircle(int a, int b, unsigned int r) : center{ point_t(a,b) }, radius{ r } {}
    potentialCircle() = delete;
    potentialCircle(const potentialCircle&) = default;
    potentialCircle(potentialCircle&&) = default;
    potentialCircle& operator=(const potentialCircle&) = default;
    potentialCircle& operator=(potentialCircle&&) = default;
    virtual ~potentialCircle() = default;
};

class Circle : public potentialCircle {
    // members detected based on Hough Circle Transform
    //point_t center;                           // coordinates of center point
    point_t alternative_center;                 // needed when center is out of frame
    //unsigned int radius;                      // radius

    // members calculated based on Flood Fill algorithm (more realistic)
    unsigned int area = 0;
    float diameter = 0;
    float perimeter = 0;
....
};

b) where should I put method which needs to compare two difference objects? one object of type Circle and one of PotentialCirle? currently, I have defined below function as part of Circle

bool Circle::is_greater(const std::pair<potentialCircle, int>& point_pair) const;

but I don't have access to protected data members of potentialCircle, although Circle is inheriting from potentialCircle. Maybe I should defined is_greater() as part of namepsace and make it a friend to Circle and potentialCircle.

Do you have better idea?

Aucun commentaire:

Enregistrer un commentaire