Review generated Polynomial class

from Operator overloading
C++23 advanced 6 min 4 issues to find

Review this generated polynomial type against the requested contract.

Own coefficients efficiently, add polynomials of different degrees by treating missing coefficients as zero, provide checked coefficient access, and compare complete values.

C++
#include <cstddef>
#include <vector>

class Polynomial {
    std::vector<double> coefficients_;
public:
    explicit Polynomial(std::vector<double> values) : coefficients_(values) {}

    Polynomial operator+(const Polynomial& other) const {
        Polynomial result{coefficients_};
        for (std::size_t i = 0; i < other.coefficients_.size(); ++i) {
            result.coefficients_[i] += other.coefficients_[i];
        }
        return result;
    }

    double operator[](std::size_t index) const {
        return coefficients_[index];
    }

    friend bool operator==(const Polynomial& left, const Polynomial& right) {
        return left.coefficients_.size() == right.coefficients_.size();
    }
};

generated code is illustrative, not from any one model

Open in playground
Report an error