本文主要是介绍引用(References),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
引用(References)
int a = 12;
int &ra = a;
int &rr1 = ra; // OK!
// int &&rr2 = ra; // error!
cout << rr1 <<endl;
rr1 = 3;
cout << a << " " << ra << " " << rr1 << endl;
//输出结果rr1: 12
// a b c: 3 3 3
// int &*pri; // error! //不允许对指针的引用
// int &ar[3]; // error! //不允许对数组的引用
References can't be const or volatile, because aliases can't be const or volatile, though a reference can refer to an entity that is const or volatile. An attempt to declare a reference const or volatile directly is an error:
int &const cri = a; // should be an error . . .
const int &rci = a; // OK
Strangely, it's not illegal to apply a const or volatile qualifier to a type name that is of reference type. Rather than cause an error, in this case the qualifier is ignored:
typedef int *PI; typedef int &RI; const PI p = 0; // const pointer const RI r = a; // just a reference!
There are no null references, and there are no references to void:
C *p = 0; // a null pointer C &rC = *p; // undefined behavior extern void &rv; // error!
这篇关于引用(References)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!