CirclePolygon的定义在Graph.h和graph.cpp中为here

对于某些练习,我需要使用shapes关键字制作一些未命名的newCirclepolygon都是Shape的种类。

例如,如果我有一个vector_ref<Circle> vc;,则可以使用以下语句在该向量中添加一个未命名的Circlevc.push_back(new Circle (Point (p), 50));,因为在定义circle时可以提供其参数(点和半径)。

但是对于polygons,主题是不同的。
对于具有polygon的对象,我必须先声明它,例如Polygon poly;,然后以这种方式在poly.add(Point(p));上添加点。现在,这给我带来了麻烦。

考虑我有一个polygons的向量,Vector_ref<Polygon> vp;现在如何像使用polygon一样使用new关键字添加(即回推)一个circle
我的代码是这样的:

#include <GUI.h>
using namespace Graph_lib;

//---------------------------------

class Math_shapes : public Window {

public:
    Math_shapes(Point, int, int, const string&);

private:
    //Widgets
    Menu menu;
    Button quit_button;
    In_box x_coor;
    In_box y_coor;

    Vector_ref<Circle> vc;
    Vector_ref<Graph_lib::Rectangle> vr;
    Vector_ref<Graph_lib::Polygon> vt;
    Vector_ref<Graph_lib::Polygon> vh;

    //Action fucntions
    void circle_pressed()     {
        int x = x_coor.get_int();
        int y = y_coor.get_int();
        vc.push_back(new Circle (Point(x,y), 50));
        attach(vc[vc.size()-1]);
        redraw();
    }
    void square_pressed()     {
        int x = x_coor.get_int();
        int y = y_coor.get_int();
        vr.push_back(new Graph_lib::Rectangle (Point(x,y), Point(x+100,y+100)));
        attach(vr[vr.size()-1]);
        redraw();
    }
    void triangle_pressed()     {
        int x = x_coor.get_int();
        int y = y_coor.get_int();
        vt.push_back(new Graph_lib::Polygon); // Problem is here!!
        attach(vt[vt.size()-1]);
        redraw();

    }
    void hexagon_pressed()     {
        int x = x_coor.get_int();
        int y = y_coor.get_int();
        Graph_lib::Polygon h;
        h.add(Point(x,y)); h.add(Point(x+50,y+50)); h.add(Point(x+50,y+80));
        h.add(Point(x,y+100)); h.add(Point(x-50,y+80)); h.add(Point(x-50,y+50));
        vh.push_back(h);
        attach(vh[vh.size()-1]);
        redraw();
    }

    void quit()               { hide(); }

    // Call-back functions
    static void cb_circle   (Address, Address pw)  { reference_to<Math_shapes>(pw).circle_pressed(); }
    static void cb_square    (Address, Address pw)  { reference_to<Math_shapes>(pw).square_pressed(); }
    static void cb_triangle  (Address, Address pw)  { reference_to<Math_shapes>(pw).triangle_pressed(); }
    static void cb_hexagon   (Address, Address pw)  { reference_to<Math_shapes>(pw).hexagon_pressed(); }
    static void cb_quit     (Address, Address pw)  { reference_to<Math_shapes>(pw).quit(); }
};

//----------------------------------------------------------------------------------

Math_shapes::Math_shapes(Point xy, int w, int h, const string& title):
    Window(xy, w, h, title),
    menu (Point(x_max()-150,70),120,30,Menu::vertical, "MathShapes"),
    quit_button (Point(x_max()-100, 20), 70,20, "Quit", cb_quit),
    x_coor(Point(x_max()-450,30),50,20,"x coordinate: "),
    y_coor(Point(x_max()-250,30),50,20,"y coordinate: ")
{

    attach(x_coor);
    attach(y_coor);
    attach(quit_button);
    menu.attach(new Button(Point(0,0),0,0,"Circle",cb_circle));
    menu.attach(new Button(Point(0,0),0,0,"Square",cb_square));
    menu.attach(new Button(Point(0,0),0,0,"Equilateral triangle",cb_triangle));
    menu.attach(new Button(Point(0,0),0,0,"Hexagon",cb_hexagon));
    attach(menu);
}

//-------------------------------------------

int main()
    try {
        Math_shapes M_s(Point(100,100), 800, 600, "Math Shapes");
        return gui_main();
}

catch(...)
{
    return 0;
}

最佳答案

您只需要保持指向多边形的指针,直到将其放入conatiner中即可:

Circle* pPoly = new Polygon();
// ...
pPoly->add(Point(p1));
// ...
pPoly->add(Point(p2));
// ...
vc.push_back(pPoly);


您可能想使用智能指针,而不是上面的原始指针,但这是您可以开始的地方。

关于c++ - 如何在C++中将“new”用于多边形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27946996/

10-12 02:07