#include <iostream>
using namespace std;
int select(const int &num)
{
// num = num - 2;
int *p = const_cast<int*>(&num);
*p = 44;
//return num;
return *p;
}
int main()
{
int a = 10;
const int b = 20;
/*case 1
int const *p1(&a);
int const *p2(&b);
cout << p1 << endl;
cout << p2 << endl;
p1 = &b;
p2 = &a;
cout << p1 << endl;
cout << p2 << endl;
*/
/*case 2
int *const p1(&a); //指向变量的常量指针
int *const p2(&b);
*/
// 常引用
const int num = 100;
const int &num_ref = num;
int num2 = 33;
// 指向同一块地址
cout << &num << endl;
cout << &num_ref <<endl;
cout << select(num) << endl;
cout << num << endl;
cout << select(num2) << endl;
cout << num2 << endl;
}
常量指针和引用的实践
最新推荐文章于 2023-08-23 22:59:52 发布