C++ 宏 - 处理两对括号

标签 c++ macros c-preprocessor

我需要预处理这段代码:

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

关于C++ 宏 - 处理两对括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14231235/

相关文章:

c++ - 编辑给定键的无序映射中的值

visual-c++ - gcc 和 C++ 预处理器可以处理的 #defines 数量是否有限制?

由于预处理器指令导致的 SWIG 错误

macros - 有没有办法用宏来计数?

c++ - 有没有办法断言当前命名空间?

c - 这个C宏是什么意思?

c - 如何制作#define 函数的循环?

c++ - OpenMP 加减少 unordered_map<string,double>

c++ - 在模块之间传递 STL 和/或 MFC 对象

C++访问冲突从dll调用函数