发布覆盖c++类绘制曲线



我正在尝试实现一个程序,当用户单击屏幕时,将从点到点绘制线条。我有一个Polyline类,它是Freeform类的子类,Freeform类是Curve类的子类。Curve超类的draw方法通常调用getPoint,它将在该特定点处获得曲线的加权点。然而,在Polyline的绘制方法的情况下,我试图覆盖曲线类,只获得用户点击的点(如你所见,Polyline类的绘制方法从不调用getPoint)。然而,当我调试代码时,我看到当我尝试绘制折线时仍然调用getPoint。有什么建议吗?

class Curve {
public:
    virtual float2 getPoint(float t)=0;
    void draw(){
        glBegin(GL_LINE_STRIP);
        for (float i = 0; i < 1; i+=.01) {
            float2 point = getPoint(i);
            float x = point.x;
            float y = point.y;
            glVertex2d(x, y);
        }
        glEnd();
        }; 
    };
class Freeform : public Curve
{
protected:
    std::vector<float2> controlPoints;
public:
    virtual float2 getPoint(float t)=0;
    virtual void addControlPoint(float2 p)
    {
        controlPoints.push_back(p);
    }
    void drawControlPoints(){
        glBegin(GL_POINTS);
        for (float i = 0; i < controlPoints.size(); i++) {
            float2 point = controlPoints.at(i);
            float x = point.x;
            float y = point.y;
            glVertex2d(x, y);
        }
        glEnd();// draw points at control points
    }
};
class Polyline : public Freeform {
public:
    float2 getPoint(float t) {
        return float2(0.0, 0.0);
    }
    //we add a control point (in this case, control point is where mouse is clicked)
    void addControlPoint(float2 p)
    {
        controlPoints.push_back(p);
    }
    //trying to override Curve draw method
    void draw(){
        glBegin(GL_LINE_STRIP);
            for (float i = 0; i < controlPoints.size(); i++) {
            float2 point = controlPoints.at(i);
            float x = point.x;
            float y = point.y;
            glVertex2d(x, y);
        }
        glEnd();
    };
};
//trying to override Curve draw method

上面的评论几乎说明了一切;试图重写void Curve::draw (),但由于成员函数先前没有声明为virtual,因此无法这样做。


解决方案
  • void Curve::draw ()申报为virtual
  • 如果c++11或更高版本,请查看(并使用)关键字override

相关内容

  • 没有找到相关文章

最新更新