1

我需要预处理这段代码:

line (0,0) (5,5)

其中 (0,0) 表示开始 x 和 y 坐标,第二个 (5,5) 表示结束 x 和 y 坐标。

我能够使用获取起始坐标

#define line(x1,y1)   myArray->shapes.push_back(new Line(x1,y1));

如何处理第二个括号?

4

2 回答 2

7

像下面这样的东西怎么样:

struct LineCreator {
  LineCreator(type_of_shapes &shapes, int x1, int y1)
    : shapes_(shapes), x1_(x1), y1_(y1)
  {}
  void operator() (int x2, int y2) {
    shapes_.push_back(new Line(x1_, y1_, x2, y2));
  }
private:
  type_of_shapes &shapes_;
  int x1_, y1_;
};

#define line(x, y) LineCreator(myArray->shapes, (x), (y))
于 2013-01-09T08:49:46.467 回答
1

将其更改为:

line (0,0,5,5)

现在您可以构造以下宏:

#define line(x1,y1,x2,y2)   myArray->shapes.push_back(new Line(x1,y1)); \
                            myArray->shapes.push_back(new Line(x2,y2));
于 2013-01-09T08:46:05.000 回答