Why are these default arguments allowed?

后端 未结 3 1664
面向向阳花
面向向阳花 2021-01-19 03:14

I\'ve found this question, and I\'m completely baffled.

The answer says b is invalid, \"Non-static members can not be used as default arguments.\". That

3条回答
  •  庸人自扰
    2021-01-19 03:54

    I think this is understandable: g_x and s_x are available at compile time but this (and therefore this->m_x) is not. You can use a trick to use m_x:

    int b(int x = -1) { return x == -1 ? m_x + 1 : x + 1; }
    

    (Of course -1 should be illegal input in this case.)

提交回复
热议问题