C++构造函数中点<function name>的含义

Meaning of dot <function name> in the constructor in C++

我正在研究 LLVM,发现了一段有趣的代码

  case ARM::BMOVPCRX_CALL: {
    EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
      .addReg(ARM::LR)
      .addReg(ARM::PC)
      // Add predicate operands.
      .addImm(ARMCC::AL)
      .addReg(0)
      // Add 's' bit operand (always reg0 for this)
      .addReg(0));

    EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
      .addReg(ARM::PC)
      .addReg(MI->getOperand(0).getReg())
      // Add predicate operands.
      .addImm(ARMCC::AL)
      .addReg(0)
      // Add 's' bit operand (always reg0 for this)
      .addReg(0));
    return;
  }

我的问题是关于 .addReg.addImm。我不会说我是 C++ 的新手,但我从未见过这种类型的代码。这是什么意思,它有什么作用?为什么会有人想做这样的事情?

这种写软件的模式叫做'method chaining' or the 'named parameter idiom'

例如,您可能有 class:

class Example {
    int a, b;

public:
    Example &a(int const a) {this->a = a; return *this;}
    Example &b(int const b) {this->b = b; return *this;}
};

int main(void) {
    Example example;
    example.a(5).b(6); //example.a is now 5, example.b is now 6
    return 0;
}

对于这个例子,应该注意没有什么可以阻止你调用 example.b(6).a(5) 来获得相同的结果。