How to call a non-const function within a const function (C++)

后端 未结 7 735
天命终不由人
天命终不由人 2020-12-10 01:06

I have a legacy function that looks like this:

int Random() const
{
  return var_ ? 4 : 0;
}

and I need to call a function within that lega

7条回答
  •  旧巷少年郎
    2020-12-10 01:56

    const_cast(this)->newCall(4)
    

    Only do this if you're certain newCall will not modify "this".

提交回复
热议问题