【问题标题】:C++ class member function not modifying memberC ++类成员函数不修改成员
【发布时间】:2018-02-09 13:40:08
【问题描述】:

我从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() 调用。(我正在逐章阅读本书,所以我还没有到达他完全讨论这一点的章节)。

【问题讨论】:

    标签: c++ class c++11


    【解决方案1】:

    问题是形状点是在构造函数中计算的,并且永远不会重置。在为s_l 设置新值时,马已经走出谷仓:形状的点并没有根据s_l 的新值重建。

    更改设置器以将点重置为新值以使其工作:

    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。您可以稍后将其设置为不同的值,但工作已经完成。您想删除所有点并重新计算它们吗?如果是这样,你需要它,它不会神奇地发生。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2011-11-15
          • 2021-01-08
          • 1970-01-01
          • 2016-07-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-29
          相关资源
          最近更新 更多