zoukankan      html  css  js  c++  java
  • C++ return

    • 一般情况下,返回类型是 void 的函数使用 return 语句是为了引起函数的强制结束,这种 return 的用法类似于循环结构中的 break 语句(第 6.10 节)的作用
    • 1 //cstdlib 头文件定义了两个预处理变量,分别用于表示程序运行成功和失败:
      2 #include <cstdlib>
      3 int main()
      4 {
      5 if (some_failure)
      6 return EXIT_FAILURE;
      7 else
      8 return EXIT_SUCCESS;
      9 }
    • 函数的返回值用于初始化调用函数处创建的临时对象。在求解表达式时,如果需要一个地方储存其运算结果,编译器会创建一个没有命名的对象,这就是临时对象。 在英语中, C++ 程序员通常用 temporary 这个术语来代替 temporary object
    • 1 // return plural version of word if ctr isn't 1
      2 string make_plural(size_t ctr, const string &word,
      3 const string &ending)
      4 {
      5 return (ctr == 1) ? word : word + ending;
      6 }
      /*我们可以使用这样的函数来输出单词的单数或复数形式。这个函数要么返回其形参 word 的副本
      要么返回一个未命名的临时string 对象,这个临时对象是由字符串 word 和 ending 的相加而产生的
      这两种情况下,return 都在调用该函数的地方复制了返回的 string 对象。
      */
    • 当返回值是引用类型时,调用函数返回结果时返回的时候没有进行复制
      1 // find longer of two strings
      2 const string &shorterString(const string &s1, const string &s2)
      3 {
      4 return s1.size() < s2.size() ? s1 : s2;
      5 }
    • 千万不能返回局部变量的引用,当函数执行完后,局部变量的存储空间会被释放,对局部对象的引用就会指向不确定的内存
       1 #include<iostream>
       2 using namespace std;
       3 int& func()
       4 {
       5     int a = 55;
       6     cout << "函数里面a的地址:" << &a<<endl;
       7     return a;
       8 }
       9 int main()
      10 {
      11     int c = func();
      12     cout << "函数返回值地址:"<< &c <<endl;
      13     cout << c;
      14     return 0;
      15 }

      //以上情况c的值为55,地址变了
      可见这样接收返回的局部变量的引用可以把它的值保留下来

    •  1 #include <iostream>
       2 using namespace std;
       3 int &c()
       4 {
       5     int a=5;
       6     cout <<&a <<endl;
       7     return a;
       8 }
       9 int main()
      10 {
      11     int *b;
      12     b = &c();
      13     cout << b << endl;
      14     cout << *b << endl;//如果是cout << b << *b << endl; 又会发现*b==5?
      15     return 0;
      16 }

      内存被释放

    •  不要返回局部变量的指针

       1 #include <iostream>
       2 using namespace std;
       3 int *c()
       4 {
       5     int a=5;
       6     cout <<&a <<endl;
       7     return &a;
       8 }
       9 int main()
      10 {
      11     int *b = c();
      12     cout << *b <<  endl << b << endl;
      13     cout << *b << endl;
      14     return 0;
      15 }



      第一次输出的时候*b是函数局部变量原来的值

      如果第一次输出地址,后面输出*b是位置的

       


       在输出前先输出一个无关的变量(a),后面又指向未知了,所以说出现上面的情况
      应该是因为函数执行完后输出的返回值的时候局部变量还没有来得及释放


      尝试用一个变量将还没有释放的内存里的值保存下来

    •  1 #include <iostream>
       2 using namespace std;
       3 int &get(int *arry, int index) { return
       4 arry[index]; }
       5 int main() {
       6     int ia[10];
       7     for (int i = 0; i != 10; ++i)
       8     {
       9         get(ia, i) = 2;
      10         cout << ia[i];
      11     }
      12 }

  • 相关阅读:
    SWT中如何居中显示?
    项目一 默认构造函数和带参数的构造函数
    解决ubuntu中java1.6显示中文乱码问题
    网络程序为什么要处理SIGPIPE
    SQL 数据类型大全
    poj1275 差分约束
    Oracle分组
    Android利用ViewPager实现滑动广告板
    扩展spring mvc的拦截器,实现AOP的环绕增加效果
    [Ext.Net]GridPanel之存储过程分页Sql版本
  • 原文地址:https://www.cnblogs.com/2020R/p/13178168.html
Copyright © 2011-2022 走看看