小编Moi*_*ñas的帖子

方法链中的C++执行顺序

该程序的输出:

#include <iostream> 
class c1
{   
  public:
    c1& meth1(int* ar) {
      std::cout << "method 1" << std::endl;
      *ar = 1;
      return *this;
    }
    void meth2(int ar)
    {
      std::cout << "method 2:"<< ar << std::endl;
    }
};

int main()
{
  c1 c;
  int nu = 0;
  c.meth1(&nu).meth2(nu);
}
Run Code Online (Sandbox Code Playgroud)

方法是:

method 1
method 2:0
Run Code Online (Sandbox Code Playgroud)

开始nu时为什么不是1 meth2()

c++ operator-precedence chaining

105
推荐指数
4
解决办法
7411
查看次数

标签 统计

c++ ×1

chaining ×1

operator-precedence ×1