宏处理两对圆括号

C++ Macro - process two pairs of parentheses

本文关键字:圆括号 处理      更新时间:2023-10-16

我需要预处理这段代码:

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));

如何处理第二个括号?

不如这样写:

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))

改为:

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));