前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++-入门语法(二)

C++-入门语法(二)

作者头像
cwl_java
发布2019-10-26 21:28:15
2680
发布2019-10-26 21:28:15
举报
文章被收录于专栏:cwl_Javacwl_Java
默认参数
  • C++允许函数设置默认参数,在调用时可以根据情况省略实参。规则如下:
    • 默认参数只能按照右到左的顺序
    • 如果函数同时有声明、实现,默认参数只能放在函数声明中
    • 默认参数的值可以是常量、全局符号(全局变量、函数名)
  • 函数重载、默认参数可能会产生冲突、二义性(建议优先选择使用默认参数)
代码语言:javascript
复制
#include <iostream>
using namespace std;

//void func(int a = 10, int b = 60, int c = 20);
//
//void func(int a, int b, int c) {
//	cout << "a is " << a << endl;
//	cout << "b is " << b << endl;
//	cout << "c is " << c << endl;
//}

int age = 70;

//void func(int a = 10, int b = 60, int c = age) {
//	cout << "a is " << a << endl;
//	cout << "b is " << b << endl;
//	cout << "c is " << c << endl;
//}

//void test() {
//	cout << "test()" << endl;
//}
//
//void display(int a, void (*func)() = test) {
//	cout << "a is " << a << endl;
//	cout << "func is " << func << endl;
//	func();
//}

//void display() {
//	cout << "display" << endl;
//}

void display(int a = 10, int b = 20) {
	cout << "a is " << a << endl;
	cout << "b is " << b << endl;
}

int main() {
	display(10);
	display(1);
	display(2);
	display(3);
	display(4);

	//display(50);

	// 指向函数的指针
	/*void (*funcPtr)() = test;
	funcPtr();*/


	getchar();

	return 0;
}
内联函数(inline function)
  • 使用inline修饰函数的声明或者实现,可以使其变成内联函数
    • 建议声明和实现都增加inline修饰
  • 特点
    • 编译器会将函数调用直接展开为函数体代码
    • 可以减少函数调用的开销
    • 会增大代码体积
  • 注意
    • 尽量不要内联超过10行代码的函数
    • 有些函数即使声明为inline,也不一定会被编译器内联,比如递归函数
代码语言:javascript
复制
#include <iostream>
using namespace std;

inline int sum(int a, int b);

int main() {
	sum(10, 20);

	getchar();
	return 0;
}

inline int sum(int a, int b) {
	return a + b;
}
代码语言:javascript
复制
#ifndef __OTHER_H
#define __OTHER_H

void other();

#endif // !abc
代码语言:javascript
复制
#ifndef __TEST_H
#define __TEST_H

void sum();
void minus();

#endif // !__TEST_H
内联函数与宏
  • 内联函数和宏,都可以减少函数调用的开销
  • 对比宏,内联函数多了语法检测和函数特性
引用(Reference)
  • 在C语言中,使用指针(Pointer)可以间接获取、修改某个变量的值
  • 在C++中,使用引用(Reference)可以起到跟指针类似的功能
  • 注意点
    • 引用相当于是变量的别名(基本数据类型、枚举、结构体、类、指针、数组等,都可以有引用)
    • 对引用做计算,就是对引用所指向的变量做计算
    • 在定义的时候就必须初始化,一旦指向了某个变量,就不可以再改变,“从一而终”
    • 可以利用引用初始化另一个引用,相当于某个变量的多个别名
    • 不存在【引用的引用、指向引用的指针、引用数组】
  • 引用存在的价值之一:比指针更安全、函数返回值可以被赋值
代码语言:javascript
复制
#include <iostream>
using namespace std;

enum Season {
	Spring,
	Summer,
	Fall,
	Winter
};

struct Student {
	int age;
};

//int age = 10;
//
//int &func() {
//	//// age .....
//	//int &rAeg = age;
//	//return rAeg;
//	return age;
//}

//void swap(int a, int b) {
//	int temp = a;
//	a = b;
//	b = temp;
//}

//void swap(int *a, int *b) {
//	int temp = *a;
//	*a = *b;
//	*b = temp;
//}

void swap(int &a, int &b) {
	int temp = a;
	a = b;
	b = temp;
}

int main() {
	int v1 = 10;
	int v2 = 20;
	swap(v1, v2);
	cout << "v1 is " << v1 << endl;
	cout << "v2 is " << v2 << endl;

	/*func() = 30;

	cout << age << endl;*/

	/*int age = 20;
	int &rAge = age;
	rAge = 30;

	int *p = &age;*/

	// 定义了一个引用,相当于是变量的别名
	/*int &rAge = age;
	int &rAge1 = rAge;
	int &rAge2 = rAge1;

	rAge = 11;
	cout << age << endl;

	rAge1 = 22;
	cout << age << endl;

	rAge2 = 33;
	cout << age << endl;*/

	/*Season season;
	Season &rSeason = season;
	rSeason = Winter;
	cout << season << endl;*/

	/*Student stu;
	Student &rStu = stu;
	rStu.age = 20;
	cout << stu.age << endl;*/

	/*int a = 10;
	int b = 20;

	int *p = &a;
	int *&rP = p;
	rP = &b;
	*p = 30;
	cout << a << endl;
	cout << b << endl;*/

	/*int array[] = { 10, 20, 30 };
	int (&rArray)[3] = array;

	int *a[4];
	int (*b)[4];*/
	

	getchar();
	return 0;
}
const
  • const是常量的意思,被其修饰的变量不可修改
    • 如果修饰的是类、结构体(的指针),其成员也不可以更改
代码语言:javascript
复制
#include <iostream>
using namespace std;

//struct Student {
//	int age;
//};

//int func() {
//	return 10;
//}

//int sum(int *a, int *b) {
//	cout << "sum(int *a, int *b)" << endl;
//	return *a + *b;
//}

int sum(const int *a, const int *b) {
	cout << "sum(const int *a, const int *b)" << endl;
	return *a + *b;
}

int main() {
	/*int age = 10;
	int &rAge = age;
	rAge = 20;*/

	/*int v1 = 10;
	int v2 = 20;
	sum(&v1, &v2);

	const int v3 = 10;
	const int v4 = 20;
	sum(&v3, &v4);*/

	/*int a = 10;
	int b = 20;
	int *p = &b;
	*p = a; 
	p = &a;*/

	// sum(50, 60);
	

	/*int a = 20;
	int b = 30;
	int age = 10; */// 4
	//const double &ref = age; // 8
	//const int &rAge = func();
	//const int &rAge = 40;
	//const int &rAge = a + b;

	// 引用的本质就是指针
	//int age = 10;
	// rAge的指向不能改
	// int & const rAge = age;

	// 不能通过引用修改所指向的内容
	/*const int &rAge = age;
	rAge = 30;*/

	// 不能通过指针修改所指向的内容
	// int const *pAge1 = &age;
	// 不能修改指针的指向,但是可以通过指针修改所指向的内容
	// int * const pAge2 = &age;


	/*const int age = 10;

	const Student stu = { 20 };
	Student stu2 = { 40 };
	stu = stu2;
	stu.age = 50;*/

	/*Student stu = { 20 };
	stu.age = 40;

	const Student *pStu = &stu;
	pStu->age = 50;*/

	//int height = 20;

	//int age = 10;
	//// *p0是常量,p0不是常量
	//const int *p0 = &age;
	//// *p1是常量,p1不是常量
	//int const *p1 = &age;
	//// p2是常量,*p2不是常量
	//int * const p2 = &age;
	//// *p3是常量,p3是常量
	//const int * const p3 = &age;
	//// *p4是常量,p4是常量
	//int const * const p4 = &age;


	/*Student stu1 = { 10 };
	Student stu2 = { 20 };

	const Student * pStu = &stu1;
	*pStu = stu2;
	(*pStu).age = 30;
	pStu->age = 40;
	pStu = &stu2;*/

	getchar();
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 默认参数
  • 内联函数(inline function)
  • 内联函数与宏
  • 引用(Reference)
  • const
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档