C++引用复习

//引用复习
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值