zoukankan      html  css  js  c++  java
  • code of C/C++ (1)

    去年,王老师拷贝给我一些代码,非常感激,老爷子的水平我这个小辈只能仰视,代码都是来自他所教的课程,有些课程因为这几年据说太难都给取消掉了,实在是 我们学校的损失。

    C/C++代码都是在讲述一些非常基本的知识,为了不至于让这些美好的代码失传,我选择一些给我这个“编程幼儿”启发的代码放到这里,这个系列会从C++延伸到C 系语言在具体领域的应用,知识点的注释是我写的,待商榷的说法欢迎大家发表 意见+参考资料 来讨论。

    //引用是给某一个变量去了一个“外号”,对它操作与对原变量的操作具有同样的效果;
    //引用 类型的变量 给它赋值 变量拥有相同的地址;
    //函数调用的过程中,参数和局部变量会被放入栈中,所以同样的变量放入函数调用中-
    //会有不同的地址值
    
    #include    <iostream>
    
    using    namespace    std;
    
    //void  func(int &, int);
    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;
        //    ca=200;
        // b= 200;
        const int cb = 300;
    
        //    func(cb, a);
    
        //    int & bb=cb;
        //    cb=400;
        //    bb=400;
    
        cout << &a << "  " << a << endl;     //&a == &b,character of & (引用的特性)
        cout << &b << "  " << b << endl;
        cout << &c << "  " << c << endl;
    
        funr(a, a);             
        funr(2, 2);
    
        int& rf = rfun(c, c);
        cout << &rf << "  " << rf << endl;
    
    }
    
    void  funr(const int &  ra, int  pa)    {  //call funr,ra,pa puted in the stack.So they have-
        cout << &ra << "  " << ra << endl;     //different address.
        cout << &pa << "  " << pa << endl;
    }
    
    int&  rfun(const int &  ra, int  pa)    {
    
        int  x = 200, &d = x;
        return  (int&)ra;
        //    return  (int&)pa;    //  warning
        //    cout<<&d<<"  "<<d<<endl;
        //    return  d;            //  warning
    }


     

  • 相关阅读:
    C++学习笔记1——const
    反转二叉树
    pywinauto 使用
    pywinauto 的使用
    爬虫基础知识
    mongdb安装配置
    pyinstaller
    Python3.6+pyinstaller+Django
    py2exe安装使用
    cx_freeze的安装使用
  • 原文地址:https://www.cnblogs.com/hanxinle/p/4833593.html
Copyright © 2011-2022 走看看