How to use a member variable as a default argument in C++?

前端 未结 4 895
感情败类
感情败类 2020-11-30 08:16

I want to make an argument for one of the member functions optional. When no argument is provided, it would use an member variable.

However, when I tried to compil

4条回答
  •  伪装坚强ぢ
    2020-11-30 08:52

    Besides overloading, another approach is passing an invalid value as the default value. Then check it inside your function.

    void Object::MoveTo(double speed, Point position = InvalidPosition) {
       position = (position != InvalidPosition) ? position : initPos;
       ...
    }
    

提交回复
热议问题