#include<iostream> //const 和 引用的值必須初始化 //等號左側是const或者const和引用,右側可以是數字,普通變量-等號左側是const和指針,右側必須是const或者引用 --- //---但是等號右側是const,則左側必須是const using namespace std; int j = 0; //這里規定i和j都必須定義在函數體外 constexpr int i = 2; //這里規定i和j都必須定義在函數體外 int main() { const int *p = nullptr; // p是一個指向整形常量 的指針 constexpr int * q = nullptr; // q是一個指向整數的 常量指針 constexpr const int *p0 = &i; constexpr int *p1 = &j; constexpr const int *p2 = &j; const int *p3 = &j;//引用的本質是一個常量指針! {//const引用讓變量擁有只讀屬性 (不能通過引用來修改原值了) int b ;//這里賦不賦初值都對 const int &a = b; } {//如果用一個對象去初始化另一個對象,則它們是不是const都無關緊要。 int c = 10; const int d = c; int e = d; } {//對常量的引用,引用及其對應的對象都是常量。 const int x = 1024; const int & y = x; // int & y1 = x; 不對 // y = 43; 不對,試圖用一個非常量引用指向一個常量對象 } {//指針引用===等號左側是const和指針,右側必須是const或者引用 int i1 = 2; const int *j = 0; const int *i2 = &i1; const int *i3 = j; } system("pause"); }
不過仍舊有疑問:
就是i和j為什么必須定義到函數體外部,並且定義在內部顯示