l-value與r-value

l-value = r-value;

在等號左邊的叫l-value,在等號右邊的叫r-value。

區別方式

  • 有名字一律為左值l-value,沒有名字一律為右值r-value
  • 可以取得記憶體位址一律為左值,沒有辦法取得記憶體位址一律為右值

l-value

lvalue simply means an object that has an identifiable location in memory

可以放在等號左邊,並且可以被指派值都是l-value。

等號左邊(l-value),也就是一個能夠擺在等號左邊的東西∶一個變數,而非常數

等號左邊可以是變數、陣列元素、結構、參考取值運算子

以下都是lavlue:

有定義資料型態(int, double, float, char, long long …)的變數,可以指派值

1
2
3
4
  // i is l-value
  int i;
  //j is l-value
  int j = 10; 	// 10 is r-value

有定義資料型態(int, double, float, char, long long …)的指標,可以指向記憶體位址。

Prerequisites:

指標基本觀念

1
2
3
4
  //p1 is l-value
  int* p1;
  //p2 is l-value
  int* p2 = &j;	//&j is r-value

可以用*取值運算子修改指標指向的記憶體位址的內容

1
2
3
4
5
6
7
8
  //j is l-value
  int j = 10; //  10 is r-value
  
  //p2 is l-value
  int* p2 = &j;   //&j is r-value
  
  //*p2 is lavlue
  *p2 = 100;  //100 is r-value

可以將變數或指標,指派給&參考

Prerequisites:

參考

參考指向指標

參考變數

1
2
3
4
  // i is l-value
  int i = 10; // 10 is r-value
  // ref is l-value
  int& ref = i;

參考指標

1
2
3
4
5
6
7
8
9
10
11
  // i is l-value
  int i = 10; // 10 is r-value
  
  //宣告指標
  // ptr_i is l-value
  int* ptr_i = &i; // &i is r-value

  //宣告參考
  // 指標指派給參考
  // ref_to_ptr is l-value
  int*& ref_to_ptr = ptr_i; // ptr_i is l-value

陣列

Prerequisites:

陣列

1
2
	// arr is l-value
  int arr[] = {0,1,2,3}; // 0,1,2,3 is r-value

陣列元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main() {
  // str is l-value
  char str[6];
  // str[0] is l-value
  str[0] = 'H'; // H is r-value
  // str[1] is l-value
  str[1] = 'e'; // e is r-value
  // str[2] is l-value
  str[2] = 'l'; // l is r-value
  // str[3] is l-value
  str[3] = 'l'; // l is r-value
  // str[4] is l-value
  str[4] = 'o'; // o is r-value
  // str[5] is l-value
  str[5] = '\0'; // \0 is r-value
  cout << str << endl;
  return 0;
}
Hello

*(陣列名 + 索引)

Prerequisites:

一維陣列與指標

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main() {
  // str is l-value
  char str[6];
  //*(str + 0) is l-value
  *(str + 0) = 'H'; // H is r-value
  //*(str + 1) is l-value
  *(str + 1) = 'E'; // E is r-value
  //*(str + 2) is l-value
  *(str + 2) = 'L'; // L is r-value
  //*(str + 3) is l-value
  *(str + 3) = 'L'; // L is r-value
  //*(str + 4) is l-value
  *(str + 4) = 'O'; // O is r-value
  //*(str + 5) is l-value
  *(str + 5) = '\0'; // \0 is r-value
  cout << str << endl;
  return 0;
}
HELLO

指標指向陣列

Prerequisites:

一維陣列與指標

1
2
3
4
  // array is l-value
  int array[5];
  // p is l-value
  int* p = array; // array is l-value

const與指標

Prerequisites:

const與指標

1
2
3
4
  //var1 is l-value
  int var1 = 10; // 10 is r-value
  // p is l-value
  const int* p = &var1; // &var1 is r-value

r-value

r-value” refers to data value that is stored at some address in memory.

等號右邊的東西可以是字串常數、表達式。

以下是r-value:

字串常數

Prerequisites:

char字串

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
  // cstr2 is l-value
  char cstr2[] = "hello"; // hello is r-value
  //cstr3 is l-value
  char cstr3[6] = "hello";// hello is r-value
  //cstr4 is l-value
  char cstr4[] = {"hello"};// hello is r-value
  //cstr5 is l-value
  char cstr5[6] = {"hello"};// hello is r-value
  //cstr6 is l-value
  char cstr6[6] {"hello"};// hello is r-value
  return 0;
}

&陣列 與 &陣列[索引]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
  // array is l-value
  int array[5];
  // array is l-value
  cout << "陣列名 = " << array << endl;
  // &array is r-value
  cout << "陣列名地址 = " << &array << endl;
  // &array[0] is r-value
  cout << "array[0]地址 = " << &array[0] << endl;
  // &array[1] is r-value
  cout << "array[1]地址 = " << &array[1] << endl;
  // &array[2] is r-value
  cout << "array[2]地址 = " << &array[2] << endl;
  // &array[3] is r-value
  cout << "array[3]地址 = " << &array[3] << endl;
  return 0;
}

指標 + 整數(0,1,2,3 …)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
  // array is l-value
  int array[5];
  // p is l-value
  int* p = array; // array is l-value
  // p is l-value
  cout << "p指標內容 = " << p << endl;
  //p + 0 is r-value
  cout << "p指標+0 = " << p + 0 << endl;
  //p + 1 is r-value
  cout << "p指標+1 = " << p + 1 << endl;
  //p + 2 is r-value
  cout << "p指標+2 = " << p + 2 << endl;
  //p + 3 is r-value
  cout << "p指標+3 = " << p + 3 << endl;
  return 0;
}
1
2
3
4
5
6
7
8
9
int main() {
  // array is l-value
  int array[5];
  // p is l-value
  int* p = array; // array is l-value
  // p is l-value
  p = p + 1; // p + 1 is r-value
  return 0;
}

函式傳回值為物件的值

以下的程式碼,main()和getStudente()都各別有存放傳回值student的記憶體位址,待getStudent()的student變數傳回給main()的student變數後,getStudent()的student變數的記憶體位址就會被釋放。

被記憶體釋放,不會被保留住的物件,也是右值r-value。

傳回物件

1
2
3
4
5
6
7
8
Student getStudent() {
  Student student;
  return student;
}
int main() {
  Student student = getStudent();
  return 0;
}

傳回值是臨時物件

臨時物件是右值。

臨時物件建立語法

類別名()
Student()
1
2
3
4
5
6
7
Student getStudent() {
  return Student();
}
int main() {
  Student student = getStudent();
  return 0;
}

results matching ""

    No results matching ""