C++类成员函数未修改成员

4

我从一个基础的Shape类(由Stroustrup编写,我正在使用编程原理和实践)中创建了一个派生的Octagon类。构造函数和其他方法似乎都没问题,只是调用修改类成员变量的函数没有任何改变。

这是我编写的派生类(遵循基类接口):

class Octagon : public Shape
{
public:
    Octagon(Point centre, int side_length);

    double cot(double x) const{return (cos(x) / sin(x));}
    void draw_lines() const;
    void set_side(int x) { s_l = x;  }       //am i missing something?

    double rad(int side){return (0.5*side*cot(PI / 8));}

private:
    int s_l;

};

Octagon::Octagon(Point centre, int side_length)
    :s_l( side_length ) 
{
    for (int ang = 22.5; ang <= 337.5; ang += 45)
    {
        add(Point{ int(centre.x + rad(s_l) * cos(ang*PI / 180)),
            int(centre.y - rad(s_l) * sin(ang*PI / 180)) });
    };
}

当我在主函数中调用set_side(int)函数时,它实际上并没有执行任何操作...

int main()
{
    Simple_window win{ Point{100,100}, 1200, 900, "window" }; //Simple_window class
                                                             //designed by Stroustrup to display Shape objects

    Octagon oct{ Point(600,450), 100};

    oct.set_color(Color::black);

    oct.set_side(20);     //supposed to change the side length from 100 to 20
                          //but it stays at 100 when i run the program

    win.attach(oct);      //attaches Shape object to Simple_window

    win.wait_for_button();

}

我不知道这是否必要,但这是由Stroustrup使用FLTK GUI库设计的Shape基类。

class Shape  {        // deals with color and style, and holds sequence of lines 
public:
    void draw() const;                 // deal with color and draw lines
    virtual void move(int dx, int dy); // move the shape +=dx and +=dy

    void set_color(Color col) { lcolor = col; }
    Color color() const { return lcolor; }
    void set_style(Line_style sty) { ls = sty; }
    Line_style style() const { return ls; }
    void set_fill_color(Color col) { fcolor = col; }
    Color fill_color() const { return fcolor; }

    Point point(int i) const { return points[i]; } // read only access to points
    int number_of_points() const { return int(points.size()); }

    virtual ~Shape() { }
protected:
    Shape();    
    virtual void draw_lines() const;   // draw the appropriate lines
    void add(Point p);                 // add p to points
    void set_point(int i,Point p);     // points[i]=p;
private:
    vector<Point> points;              // not used by all shapes
    Color lcolor;                      // color for lines and characters
    Line_style ls; 
    Color fcolor;                      // fill color

    Shape(const Shape&);               // prevent copying
    Shape& operator=(const Shape&);
};

顺便说一下,draw_lines() 是由显示引擎调用的 draw() 函数调用的。(我按照书本逐章学习,所以还没有到他完全讨论这个问题的章节)。
3个回答

4

你只在构造函数中使用了s_l。虽然可以稍后将其设置为不同的值,但工作已经完成。如果要删除所有点并重新计算它们,那么你需要手动进行,这不会自动发生。


4
问题在于形状点是在构造函数中计算的,并且从未重置。当为 s_l 设置新值时,已经为时过晚:形状的点没有考虑到 s_l 的新值而重新构建。
将 setter 更改为重置点以使用其新值使其正常工作:
void set_side(int x) {
     s_l = x;
     int pos = 0;
     for (double ang = 22.5 ; ang <= 337.5 ; ang += 45) {
         set_point(pos++, Point {
             int(centre.x + rad(s_l) * cos(ang*PI / 180))
         ,   int(centre.y - rad(s_l) * sin(ang*PI / 180))
         });
    };
}

请注意上面的ang应该是double而不是int

2

显然,当您修改了s_l之后,您就没有重新计算过点数。一个简单的解决方法是将点创建代码从构造函数中移出,放入一个单独的方法中,该方法既从构造函数中调用,也从set_side中调用(后者也应首先删除先前存在的点)。或者,如果它足够简单,您可以在set_side()中修改现有的点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接