Home Article 引用的基本语法及注意事项

引用的基本语法及注意事项

Release time:2020-07-24 20:09:51 Author:admin Reading volume:67

#include

using namespace std;
/*
int main()
{
    //作用:起别名
    //引用的基本语法
    //数据类型 &别名 = 原名
    int a  =10;
    //创建引用
    int &b = a;
 
    cout << "a =" << a << endl;
    cout << "b =" << b << endl;
    //a = 10,b = 10;
 
    b = 100;
    cout << "a =" << a << endl;
    cout << "b =" << b << endl;
    // a = 100, b =100;
}*/
 
//引用注意事项
//1.引用时必须初始化   int &b;//错误的
//2.引用初始化后就不可以再更改
int main()
{
    int a = 10;
//1.引用时必须初始化   int &b;//错误的
//int &b;
    int &b = a;
//2.引用初始化后就不可以再更改
    int c = 20;
    b = c;//赋值操作而不是更改引用
    cout << "a =" << a << endl;
    cout << "b =" << b << endl;
    cout << "c =" << c << endl;
    // a , b, c都是20
 
    return 0;
}
 
支付宝打赏 微信打赏
  
I want to comment

Search

Leave a message
http://blog.rjxj513.com/
User login
You have not written any reviews yet!
You have commented!
Can only praise once!
You have a collection!