side effect

来源:互联网 发布:天津企业seo 编辑:程序博客网 时间:2024/05/16 11:30

http://www.learncpp.com/cpp-tutorial/33-incrementdecrement-operators-and-side-effects/

A side effect is a result of an operator, expression, statement, or function that persists even after the operator, expression, statement, or function has finished being evaluated.

Side effects can also be dangerous:

1
2
int x = 5;
int nValue = Add(x, ++x);

C++ does not define the order in which function parameters are evaluated. If the left parameter is evaluated first, this becomes a call to Add(5, 6), which equals 11. If the right parameter is evaluated first, this becomes a call to Add(6, 6), which equals 12!


Note that side effects are not confined to operators, expressions, and statements. Functions can also have side effects