3.3 자체 증가 / 자체 감소 연산 자, 그리고 Side effects

3688 단어 effect
from http://www.learncpp.com/cpp-tutorial/33-incrementdecrement-operators-and-side-effects/
하나의 변 수 는 1 을 늘 리 는 것 과 1 을 줄 이 는 것 이 이렇게 평범 해서 C 에서 자신의 조작 자 를 가지 게 되 었 다.모든 조작 자 는 접두사 와 접두사 두 가지 버 전이 있다.
Operator
Symbol
Form
Operation
Prefix increment
++
++x
Increment x, then evaluate x
Prefix decrement
––
––x
Decrement x, then evaluate x
Postfix increment
++
x++
Evaluate x, then increment x
Postfix decrement
––
x––
Evaluate x, then decrement x
예:
   1: int x = 5;
   2: int y = ++x; // x is now equal to 6, and 6 is assigned to y

 
   1: int x = 5;
   2: int y = x++; // x is now equal to 6, and 5 is assigned to y

 
다시 한 번 예 를 들 어 전치 와 전치 사이 의 차 이 를 표시 합 니 다.
   1: int x = 5, y = 5;
   2: cout << x << " " << y << endl;
   3: cout << ++x << " " << --y << endl; // prefix
   4: cout << x << " " << y << endl;
   5: cout << x++ << " " << y-- << endl; // postfix
   6: cout << x << " " << y << endl;

 
결 과 는:
5 5

6 4

6 4

6 4

7 3

 
Side effects
A side effect is a result of an operator, expression, statement, or function that presists even after the operator, expression, statement, or function has finished being evaluated. 
(표현 식, 함수, 구문 등 값 을 구 한 결과 로 해석 할 수 있 습 니까?)
예:
   1: x = 5;

 
쓸모 가 있다.
그리고:
   1: int x = 5;
   2: int nValue = Add(x, ++x);

 
C + + 는 함수 에서 어떤 인자 가 먼저 값 을 구 하 는 지 정의 하지 않 았 습 니 다.두 가지 결과 가 발생 할 수 있 습 니 다. 1. Add (5, 6)   2、Add(6,6).
유사 한 결 과 를 일 으 킬 수 있 는 조작 자 를 사용 하지 않 는 것 이 일반적인 규칙 이다.모든 할당 연산 자, 자체 증가, 자체 감소.side effect 를 일 으 킬 수 있 는 조작 부 호 는 독립 된 문장 에 넣 어야 하기 때 문 입 니 다.
누가 side effects 를 설명 할 수 있 습 니까?

좋은 웹페이지 즐겨찾기