引用(五)
来源:互联网 发布:linux执行文件命令 编辑:程序博客网 时间:2024/06/07 05:22
///看了个引用的小程, 发现我需要复习了。。。。。。#include<iostream>using namespace std;#include<string>struct free_throws{ std::string name; int made; int attempts; float percent;};void display(const free_throws & ft);void set_pc(free_throws & ft);free_throws & accumulate(free_throws & target, const free_throws & source);int main(){ free_throws one = {"aaa", 13, 14}; free_throws two = {"bbb", 10, 16}; free_throws three = {"ccc", 7, 9}; free_throws four = {"ddd", 5, 9}; free_throws five = {"eee", 6, 14}; free_throws team = {"fff", 0, 0}; free_throws dup; set_pc(one); display(one); accumulate(team, one); display(team); display(accumulate(team, two)); accumulate(accumulate(team, three), four); display(team); dup = accumulate(team, five); std::cout << "Displaying team:\n"; display(team); std::cout << "Displaying dup after assignment:\n"; display(dup); set_pc(four); accumulate(dup, five) = four; std::cout << "Displaying dup after ill-advised assignment:\n"; display (dup); return 0;}void display(const free_throws & ft){ using std::cout; cout << "Name: " << ft.name <<endl; cout << " Made: " << ft.made <<"\t"; cout << " Attempts: " << ft.attempts << "\t"; cout << " Precents: " << ft.percent << "\n";}void set_pc(free_throws & ft){ if(ft.attempts != 0) ft.percent = 100.0f * float(ft.made)/float(ft.attempts); else ft.percent = 0;}free_throws & accumulate(free_throws & target, const free_throws & source){ target.attempts += source.attempts; target.made += source.made; set_pc(target); return target;}/*Name: aaa Made: 13 Attempts: 14 Precents: 92.8571Name: fff Made: 13 Attempts: 14 Precents: 92.8571Name: fff Made: 23 Attempts: 30 Precents: 76.6667Name: fff Made: 35 Attempts: 48 Precents: 72.9167Displaying team:Name: fff Made: 41 Attempts: 62 Precents: 66.129Displaying dup after assignment:Name: fff Made: 41 Attempts: 62 Precents: 66.129Displaying dup after ill-advised assignment:Name: ddd Made: 5 Attempts: 9 Precents: 55.5556Process returned 0 (0x0) execution time : 0.689 sPress any key to continue.*////下面对以前对引用的总结做一些补充,前面我们已经讲到了左值引用,///而C++11新增了另一种引用///右值引用。 这种引用可指向右值。用&&声明。double && rref = std::sqrt(36.00); /// not allowed for double &double j = 15.0;double && jref = 2.0*j + 18.5;/// not allowed for double &std::cout << rref << endl;///display 6.0std::cout << jref << endl;///display 48.5
常规(非引用)返回类型是右值,即不能通过地址访问的值。其他右值包括字面值(如10.0)和表达式(如x+y)。常规函数表达式返回值为右值,是因为这种返回值位于临时内存单元中。运行到下一条语句时, 他们可能不再存在。
0 0
- 引用(五)
- 引用 vc++ 实战oledb编程(五)
- 引用 Weka学习五(ROC简介)
- 【引用】ubuntu服务器配置-(五)
- 五.引用类型
- 五:引用基础知识
- 引用 vc++ 实战oledb编程(五)(转)
- JNI学习(五)、引用、缓存、异常、多线程
- Python学习笔记(五)Python动态类型和引用
- 程序实践系列(五)引用和friend
- Shell教程(五):替代、引用机制、输入输出重定向
- JS高级程序设计笔记(五):引用类型
- 使用Spring(五) 引用其它的bean(协作者)
- 正则表达式高级用法---五(反向引用 /1, /2...)
- 正则表达式-回溯引用,前后一致匹配(五)
- Android JNI开发摘录(五)之对象引用处理
- 【Dongle】【Java】基础(五)之包及引用
- JavaScript基础(五)--- 基本数据类型和引用数据类型
- Hibernate初识
- CUDA中一些error的解决方法
- html字体大小em
- JavaScript Date对象
- RDD原理与详解
- 引用(五)
- 86. Partition List
- php设计模式 抽象工厂模式
- 用Swagger调用Harbor Registry的REST API
- 理解图像的傅里叶变换(细心分析)
- Linux安装其他版本GCC的方法
- php namespace
- 103. Binary Tree Zigzag Level Order Traversal
- 李菲菲课程笔记:Deep Learning for Computer Vision – Introduction to Convolution Neural Networks