1. 任何情况下,不能使用指向空值的引用,引用必须指向某些对象
int *p=0; //set the pointer null
int &q =*p; //quote point to null ,harmful
2. 在申明引用的时候必须初始化
string &str; //error,must initialize
string str="it is ok";
string &strRef=str //Ok!
string *str // is ok but dangerous
3. 不存在执行空值的引用,所以在使用引用时,不需要测试它的合法性
void printDouble( const std::double &refd)
{
std::cout<<refd<<std::endl;
}
void printDouble(const std::double *pntd)
{
if(pntd);
std::cout<<*pntd<<std::endl;
}
4. 引用初始化时与变量绑定,以后不会改变
string s1="hai";
string s2="zhang";
stirng &r1=s1;
string *p1=&s1;
r1=s2;// r1仍指s1,但它的值为"zhang",s1变了
p1=&s2; //p1指向s2,s1没有变