'C++'에 해당되는 글 1건

lvalue, rvalue

C++ 2018. 5. 25. 10:11

lvalue : const를 포함한 모든 변수


rvalue : 해당 값을 사용하는 식을 제외한 곳에서는 유지되지 않는 임시 값


예제


https://msdn.microsoft.com/ko-kr/library/f90831hc.aspx

// Correct usage: the variable i is an lvalue.  
   i = 7;  
  
   // Incorrect usage: The left operand must be an lvalue (C2106).  
   7 = i; // C2106  
   j * 4 = 7; // C2106  
  
   // Correct usage: the dereferenced pointer is an lvalue.  
   *p = i;   
  
   const int ci = 7;  
   // Incorrect usage: the variable is a non-modifiable lvalue (C3892).  
   ci = 9; // C3892  
  
   // Correct usage: the conditional operator returns an lvalue.  
   ((i < 3) ? i : j) = 7;  



참조 연산자


& : lvalue 참조


&& : rvalue 참조



생성자 오버로딩 등에서도 사용할 수 있음


string str = string("h") + "e" + "l" + "l" + "o";


operator+를 호출할 때마다 새 임시 string 개체(rvalue)가 할당되고 반환됩니다. operator+는 소스 문자열이 lvalue인지 아니면 rvalue인지를 알 수 없으므로 한 문자열을 다른 문자열에 추가할 수 없음


즉, operator+ 오버로딩에서 &&을 사용하여 rvalue 참조를 처리하였기 때문에 2010 버전 이후로는 오류 발생 X

블로그 이미지

NCookie

,