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

前端 未结 4 896
感情败类
感情败类 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条回答
  •  旧时难觅i
    2020-11-30 09:14

    Default values are not part of the prototype, i.e. they're resolved by the caller, not by the function itself. So firstly, they have to be visible to the caller. Secondly, they cannot access protected members of the class. (I'm pretty sure you can't even use public members as defaults, but I'm too tired to check.)

    To solve the problem, use chained overloads as suggested in other answers.

提交回复
热议问题