如何在main()函数之外创建一个形状

时间:2014-09-08 14:43:37

标签: c++ visual-c++ fltk

我知道如何定义一个形状(这里是一个矩形)并将其附加到C ++窗口中,如下所示:

  #include <Simple_window.h>

void cir() { Circle c(Point(100,100),50); }

int main()
{
  Simple_window win(Point(100,100),600,400, "test");

  Rectangle r(Point(100,100),Point(300,200));
  win.attach(r);
  win.wait_for_botton();

}

但如何定义一个形状(比circle cir()之外的main()函数定义win 函数是如何在cir()函数内创建一个圆圈,当我在main()函数中调用它时它返回那个圆圈,这样我就可以将它附加到窗口{{1}}上以便可见?

PS:我刚刚通过PPP书(this)学习了C ++,直到第14章结束: - )

1 个答案:

答案 0 :(得分:0)

或许这样的事情?

  #include <Simple_window.h>

  Circle c(Point(100,100),50);

  int main()
  {
    Simple_window win(Point(100,100),600,400, "test");

    Rectangle r(Point(100,100),Point(300,200));
    win.attach(r);
    win.attach(c);
    win.wait_for_botton();
  }

或者,如果你想使用你的cir函数:

Circle cir()
{
  Circle c(Point(100,100),50);
  return c;
}

int main()
{
  Simple_window win(Point(100,100),600,400, "test");

  Rectangle r(Point(100,100),Point(300,200));
  win.attach(r);
  win.attach(cir());
  win.wait_for_botton();
}

或许你想做这样的事情,你的问题很难知道:

// The & is important, it will pass this argument as a reference
void attachCircleToWindow(Simple_window &win) 
{
  Circle c(Point(100,100),50);
  win.attach(c);
}

int main()
{
  Simple_window win(Point(100,100),600,400, "test");

  Rectangle r(Point(100,100),Point(300,200));
  win.attach(r);
  attachCircleToWindow(win);
  win.wait_for_botton();
}