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

C++ 之引用

时间:2016-05-19 01:25:34      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

  1. int argc ,char * argv[] - argument count & argument vector

    argc - 命令行参数个数,argv[]依次指向每一个命令行参数,其中argv[0]为程序名字:下面的程序包含了完整的程序路径。

    #include <iostream>
    
    int main(int argc, char *argv[])
    {     int i = 0;                 // begin with 0
          while (i < argc)
              std::cout << argv[i++] // output string
                   << " "            // output SPACE
                   << std::endl;     // terminate output line 
          return 0;
    }

     

  2. 引用

    1)作为返回值,不需要产生临时对象,然后拷贝这个对象,(见下面代码的注释),目前不要深究引用机制,目前记住的返回引用的场景:输入流、 //发现其他的再补充

    #include    <iostream>
    using namespace std;
    
    
    int & rfun(int &);                                                     //don‘t dig more,ref func saves time.
    int fun(int);
    
    void  main(void) {
        int  a = 7, b = 9; 
        int &ra = a, &rb = b;                                          //Treat ra as another name of a
        int *pa = &ra, *pb = &rb;
    
        cout << pa << "  " << (void *)pa << endl;
        cout << pb << "  " << (void *)pb << endl;
    
        cout << a << "  " << b << endl;
        cout << rb << "  " << (void *)&rb << endl;                      //&rb == pb always.
        rb = rfun(ra);                                 //b=ra;  rb=ra;  -->  int &rd=a;
                                                    //rb=fun(ra);
        cout << rb << "  " << (void *)&rb << endl;
    
        cout << a << "  " << b << endl;
        rb = 8;
        cout << a << "  " << b << endl;
    }
    
    int & rfun(int & r) {
        return  r;        //  b=fun(a)  -->  b=a;
    };
    
    int fun(int r) {
        return    r;        //  b=fun(a)  -->  r=a;  b=r;
    };
    2)引用类型参数

    对于class,vector等类型的参数,引用 避免了值拷贝,提升效率,使用引用,函数可以改变实参的值,如果在调用过程中实参值不会发生变化,那么添加 const 修饰。

         

//call by ref can change the value after func called.
#include    <iostream>

using    namespace    std;

void  funr(const int &, int);
int&  rfun(const int &, int);

void  main(void) {
    int a = 100;
    int& b = a;
    const  int& c = 20;

    const int & ca = a;

    a = 200;

    const int cb = 300;

    cout << &a << "  " << a << endl;
    cout << &b << "  " << b << endl;
    cout << &c << "  " << c << endl;

    cout << endl << endl;

    funr(a, a);                           //first a,like above;second a, int tmp = a;
    funr(2, 2);
    cout << endl << endl;

    int& rf = rfun(c, c);
    cout << &rf << "  " << rf << endl;

}

void  funr(const int &  ra, int  pa) {    
    cout << &ra << "  " << ra << endl;
    cout << &pa << "  " << pa << endl;    //int tmp = pa, &pa is addr of tmp
}

int&  rfun(const int &  ra, int  pa) {    // b = rfun(ra,pa)-->b = ra

    int  x = 200, &d = x;
    cout << &ra << "  " << ra << endl;
    cout << &pa << "  " << pa << endl;
    return  (int&)ra;
}

 以上则是目前想到的关于引用的知识点小结,以前曾经写过这话题的博文,今天讲解细致些,看注释,运行程序,更快速理解引用,VS2015社区版。

C++ 之引用

标签:

原文地址:http://www.cnblogs.com/hanxinle/p/5507136.html

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