ITPub博客

首页 > > > c++返回引用

c++返回引用

作者:友哥 时间:2018-08-09 17:18:00 0 删除 编辑
#include <iostream>
#include <ctime>
 
using namespace std;
 
double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};
 
double[]& setValues( int i ) {
   return vals;   // return a reference to the ith element
}
 
// main function to call above defined function.
int main () {
 
   cout << "Value before change" << endl;
   for ( int i = 0; i < 5; i++ ) {
      cout << "vals[" << i << "] = ";
      cout << vals[i] << endl;
   }
 
   setValues(1)[1] = 20.23; // change 2nd element
   setValues(3)[2] = 70.8;  // change 4th element
 
   cout << "Value after change" << endl;
   for ( int i = 0; i < 5; i++ ) {
      cout << "vals[" << i << "] = ";
      cout << vals[i] << endl;
   }
   return 0;
}

 

原文作者:;如有侵权,请联系管理员删除。

下一篇: MySQL锁监视器
请登录后发表评论 登录
全部评论

注册时间:2020-05-20

  • 博文量
    276
  • 访问量
    1000