码迷,mamicode.com
首页 > 编程语言 > 详细

C++引用复习

时间:2015-04-25 22:50:54      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

//引用复习
#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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!