ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

day01_cin、cout、函数重载、extern C、默认参数

2021-04-17 23:03:38  阅读:134  来源: 互联网

标签:__ 函数 int day01 cin 默认 参数 extern


cin &cout

#include <iostream>
using namespace::std;
int main(int argc, const char * argv[]) {
    // insert code here...
    int age;
    cin >> age;
    cout << "Hello, World!\n" << endl;
    return 0;
}

cin用的右移运算符>>,cout用的是左移运算符<< ,endl是换行的意思

函数的重载

  • 规则
    • 函数名相同
    • 参数个数不同、参数类型不同、参数顺序不同
  • 注意
    • 返回值类型与函数重载无关
    • 调用函数时,实参的隐式类型转换可能会产生二义性
  • 本质
    • 采用了name mangling或者叫name decoration技术
      • ✓ C++编译器默认会对符号名(变量名、函数名等)进行改编、修饰,有些地方翻译为“命名倾轧”
      • ✓ 重载时会生成多个不同的函数名,不同编译器(MSVC、g++)有不同的生成规则
      • ✓ 通过IDA打开【VS_Release_禁止优化】可以看到

默认参数

  • C++允许函数设置默认参数,在调用时可以根据情况省略实参。规则如下:
    • 默认参数只能按照右到左的顺序
    • 如果函数同时有声明、实现,默认参数只能放在函数声明中
    • 默认参数的值可以是常量、全局符号(全局变量、函数名)
#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();*/
	return 0;
}

  • 函数重载、默认参数可能会产生冲突、二义性(建议优先选择使用默认参数)
void dispay(int a,int b = 10) {
    cout << "a is" << a << endl;
}

void dispay(int a) {
    cout << "a is" << a << endl;
}
正确 :dispay(19,10);
错误 :dispay(19);

extern

◼有时也会在编写C语言代码中直接使用extern “C” ,这样就可以直接被C++调用

C++在调用C语言API时,需要使用extern "C"修饰C语言的函数声明

.cpp
//extern "C" {
	#include "sum.h"
//}

有时也会在编写C语言代码中直接使用extern “C” ,这样就可以直接被C++调用


#ifndef __SUM_H
#define __SUM_H

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

int sum(int a, int b);
int minus(int a, int b);

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // !__SUM_H

我们经常使用#ifndef、#define、#endif来防止头文件的内容被重复包含

pragma once也可以防止整个文件的内容被重复包含

  • 区别
    • "#ifndef、#define、#endif受C\C++标准的支持,不受编译器的任何限制
    • 有些编译器不支持#pragma once(较老编译器不支持,如GCC 3.4版本之前),兼容性不够好
    • "#ifndef、#define、#endif可以针对一个文件中的部分代码,而#pragma once只能针对整个文件

标签:__,函数,int,day01,cin,默认,参数,extern
来源: https://www.cnblogs.com/kunpengapple/p/14672407.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有