C++ Primer Plus 第八章知识点(一)

C++ Primer Plus 第八章知识点简化

1. inline 内联函数

归纳点

  • inline函数优点及原理
  • 内联与宏的区别 (自行阅读见p255)

       内联函数是为了提高运行速度做的一项改进,其原理以及常规函数与内联函数之间的区别如下图所示:

在这里插入图片描述

对于常规函数,需要跳转到另一个位置去执行完代码,然后再跳转回来,这个就会多一个处理函数调用机制时间,而内联函数则不用跳转,耗费的时间也只有执行函数代码时间

注意点

  • 内联函数不能递归;
  • 并不是所有函数加了inline就能成为内联函数。

 


2. 引用变量

归纳点

  • 创建引用变量int a=0; int& b=a;(而且必须初始化)
  • 引用传递

 
①引用是什么?

#include <iostream>
using namespace std;

int main(){	
	int a = 101;
	int & b = a;		// rodents is a reference

	cout << "a = " << a << ", b = " << b << endl;
	cout << "a's address = " << &a << ", b's address = " << &b << endl;

	int c = 50;
	b = c;				// can we change the reference?
	cout << "c = " << c << ", a = " << a << ", b = " << b << endl;
	cout << "c's address = " << &c << ", b's address = " << &b << endl;
	return 0;
}

输出:
a = 101, b = 101
a's address = 000000D6352FF744, b's address = 000000D6352FF744
c = 50, a = 50, b = 50
c's address = 000000D6352FF784, b's address = 000000D6352FF744

笔记
 
 
 
 
 


②引用传递、指针传递和值传递

#include <iostream>
using namespace std;

void swapr(int & a, int & b);   // a, b are aliases for ints
void swapp(int * p, int * q);   // p, q are addresses of ints
void swapv(int a, int b);       // a, b are new variables

int main(){
	int wallet1 = 300, wallet2 = 350;
	cout << "wallet1 = $" << wallet1 << " wallet2 = $" << wallet2 << endl;

	cout << "Using references to swap contents:\n";
	swapr(wallet1, wallet2);		// use references
	cout << "wallet1 = $" << wallet1 << " wallet2 = $" << wallet2 << endl;

	cout << "Using pointers to swap contents again:\n";
	swapp(&wallet1, &wallet2);		// use points
	cout << "wallet1 = $" << wallet1 << " wallet2 = $" << wallet2 << endl;

	cout << "Trying to use passing by value:\n";
	swapv(wallet1, wallet2);		// use values
	cout << "wallet1 = $" << wallet1 << " wallet2 = $" << wallet2 << endl;
	return 0;
}

void swapr(int & a, int & b){
	int temp = a;       
	a = b;
	b = temp;
}
void swapp(int * p, int * q){
	int temp = *p;      
	*p = *q;
	*q = temp;
}
void swapv(int a, int b){
	int temp = a;      
	a = b;
	b = temp;
}

输出:
wallet1 = $300 wallet2 = $350
Using references to swap contents:
wallet1 = $350 wallet2 = $300
Using pointers to swap contents again:
wallet1 = $300 wallet2 = $350
Trying to use passing by value:
wallet1 = $300 wallet2 = $350

笔记
 
 
 
 
 


③引用用于结构体(很简单就不展示 --> p265)
 
 
 
 


④引用返回的时候不能是临时对象

#include <iostream>
#include <string>
using namespace std;

string version1(const string& s1, const string& s2);
const string& version2(string& s1, const string& s2);  // has side effect
const string& version3(string& s1, const string& s2);  // bad design

int main(){
	string input, copy, result;

	cout << "Enter a string: ";
	getline(cin, input);
	copy = input;
	cout << "Your string as entered: " << input << endl;
	result = version1(input, "***");
	cout << "Your string enhanced: " << result << endl;
	cout << "Your original string: " << input << endl;

	result = version2(input, "###");
	cout << "Your string enhanced: " << result << endl;
	cout << "Your original string: " << input << endl;

	cout << "Resetting original string.\n";
	input = copy;					
	result = version3(input, "@@@");
	cout << "Your string enhanced: " << result << endl;
	cout << "Your original string: " << input << endl;
	return 0;
}

string version1(const string& s1, const string& s2){
	string temp = s2 + s1 + s2;
	return temp;
}

const string& version2(string& s1, const string& s2){
	s1 = s2 + s1 + s2;
	// safe to return reference passed to function
	return s1;
}

const string& version3(string& s1, const string& s2){
	string temp = s2 + s1 + s2;
	// unsafe to return reference to local variable
	return temp;
}

输出:
Enter a string: hello world
Your string as entered: hello world
Your string enhanced: ***hello world***
Your original string: hello world
Your string enhanced: ###hello world###
Your original string: ###hello world###
Resetting original string.
然后程序崩溃

问题就出在第三个函数,该函数返回的是temp,而我们知道这个temp只是一个临时对象,执行完version3就被释放,返回指向temp的引用就不可用,所以崩溃;第二个函数就不一样,返回的不是指向临时对象的引用。

笔记
 
 
 
 
 


注意点

  • 引用传递可能会修改数值,对于这个易错地方,学会使用const
  • 不可返回一个指向临时对象的引用。

Reference:

  • 《C++ Primer Plus》第六版 Stephen Prata 著
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值