Операторы перегрузки в производном классе

Нужно ли мне переопределить все операторы перегрузки с производным типом, если мне нужно использовать их в производном классе?

Следующий код компилируется нормально:

class Point {

public:

    Point(int X = 0, int Y = 0):x(X), y(Y) {}

    virtual ~Point() {}

    Point operator +(Point &rhs) {
        return Point(x + rhs.x, y + rhs.y);
    }

protected:
    int x, y;
};

class Vector : public Point {

public:

    Vector(int X, int Y) : Point(X, Y) {}

    ~Vector() {}

    Vector operator +(Vector &rhs) {
        return Vector(x + rhs.x, y + rhs.y);
    }
};

int main()
{
    Vector v1(1, 2);
    Vector v2(3, 2);

    Vector v3 = v2 + v1;
}

Но из того, что я прочитал,

Учебник по C ++, 4-е изд. Раздел 15.5.3.

Если производный класс хочет сделать все перегруженные версии доступными через его type, то он должен либо переопределить их все, либо ни один из них.

Является ли часть цитаты " Prawn::Document.generate("samle.pdf") do text "hello #{@buyer.name} world" end but this obviously doesn't work (only if I use class ...

I'm trying to generate pdf using Prawn

@buyer = Buyer.last
Prawn::Document.generate("samle.pdf") do
  text "hello #{@buyer.name} world"
end

but this obviously doesn't work (only if I use class variable @@buyer), my question is what is the proper way of passing variable to Prawn::Document.generate

(I know the solution to this is prawnto but I'm experimenting little bit ...and also it's a sinatra project)

6
задан equivalent8 16 April 2011 в 09:12
поделиться