3 ms·
> First of all, there will be a lot of false positives. For example, you might write total_cost = p->base_price + p->calculate_tax(); > This would raise t
by euyyn 9y ago
> First of all, there will be a lot of false positives. For example, you might write
total_cost = p->base_price + p->calculate_tax();
> This would raise the warning because the compiler observes that the calculate_tax method is not const, so it is worried that executing the method may modify the base_price, in which case it matters whether you add the tax to the original base price or the updated one. Now, you may know (by using knowledge not available to the compiler) that the calculate_tax method updates the tax locale for the object, but does not update the base price, so you know that this is a false alarm.
I think the warning in that case is still fair, and I'd be perfectly happy to get it. calculate_tax() could be modified to affect base_price with this compilation unit being none the wiser.
- corpMaverick 9y agoAgree. calculate_tax should be declared const. Or it should not be used on an expression like that.