//引用复习
#include<iostream>
using namespace std;
void show1()
{
cout << "show1" << endl;
}
void show2()
{
cout << "show2" << endl;
}
void show3()
{
cout << "show3" << endl;
}
int main()
{
int one = 1;
int &r1(one); //左值引用,引用的内存实体
int &&r2(one+1);//右值引用,引用的寄存器中的值
int &&r3(move(one));//move 可以把左值作为右值引用
cout << one << " " << r1 << " " << r2 << " " << r3 << endl;
int two = 1;
int* p(&two);
int* (&rp)(p);//一级指针的引用形式
int** pp(&p);
int (**(&rpp))(pp); //二级指针的引用形式
int* && temp(&two); //对变量取地址是右值,需用右值引用
cout << *rp << " " << **rpp << endl;
void(*a)()(show1); //函数指针
a();
void(*&aa)()(a); //函数指针引用
aa();
void(*ra[3])(){show1, show2, show3}; //栈上的函数指针数组
for(auto i:ra)
{
i();
}
void(*(&rra)[3])()(ra); //引用栈上的函数指针数组
for(auto i:rra)
{
i();
}
void(**raa)() = new (void(*[3])()){show1, show2, show3}; //堆上的函数指针数组
for(int i = 0; i < 3; i++)
{
raa[i]();
}
void(**(&rraa))()(raa);
for(int i = 0; i < 3; i++) //引用堆上的函数指针数组
{
rraa[i]();
}
return 0;
}C++引用复习
原文:http://blog.csdn.net/linukey/article/details/45274447