//函数重载之名字修饰
#if 0
#include<iostream>
using namespace std;`在这里插入代码片`
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
int main()
{
add(3, 4);
add(3.03, 3.14);
cout << add(3, 4) << endl;
cout << add(3.03, 3.14) << endl;
system("pause");
return 0;
}
#endif
//1、名字修饰
//在C语言中,名字修饰只是在函数名前加下划线,所以只要函数名相同,就会导致冲突。
//在C++中,名字修饰是由“ ? 函数名@域名1@域名2...@@参数列表@Z”的格式构成的,包含:
//a、函数名
//b、所在域
//c、参数列表
//所以C++中,以上三个必须完全相同,才会出现冲突,这就是函数重载的原理
#if 0
#include<iostream>
using namespace std;
extern "C"
{
int add(int a, int b)
{
return a + b;
}
}
int main()
{
add(3, 4);
cout << add(3, 4) << endl;
system("pause");
return 0;
}
#endif
//2、extern "C"
//使用extern "C"修饰一个语句或者将一段代码包起来,那么这条语句或这段代码将会以C语言的风格进行编译
#include<iostream>
using namespace std;
int& TestRefReturn(int& z)//做返回值
{
z+= 10;
return z;
}
void SwapaArgs(int &x,int &y)//做参数
{
int tmp = 0;
tmp = x;
x = y;
y = tmp;
}
int main()
{
int a = 10;
int c = 5;
int &b = a;
int &x = a;
int &y = c;
cout << a << " " << b<<endl;
cout << x<< " " << y<< endl;
cout << TestRefReturn(a) << endl;
system("pause");
return 0;
}
//一、引用
//引用是给一个变量起别名,两个名字都是一个变量,所以操作谁从结果上看都一样。
//引用是代替指针完成跨栈操作的,所以它具备指针跨栈的一切特点。
//引用的底层实现实际是指针
//特点:
//1、引用在定义时必须初始化
//2、一个变量可以有多个引用
//3、一个引用一旦引用了一个变量,就不能再引用其他变量了
#include <iostream>
#include <typeinfo>
using namespace std;
int func1(int &a)
{
return a;
}
inline int &func2(int &a)
{
return a;
}
inline int *func3(int * pa)
{
return pa;
}
int main()
{
int a = 3;
cout << func1(a) << endl;
cout << (func2(a) = 4) << endl;
//system("pause");
//*func3(&a) = 5;
cout << (*func3(&a) = 5) << endl;
system("pause");
return 0;
}
//二、内联函数
//调用时不创建新栈而直接在调用处展开的函数叫内联函数,关键字为inline。
//内联函数是一个对编译器的建议,如果函数过于复杂,编译器会不接受你的建议,而处理成普通的函数。
//inline函数可以代替带参宏``
#include<iostream>
#include <typeinfo>
using namespace std;
int main()
{
//用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&
int x = 10;
auto a = &x;
auto* b = &x;
auto& c = x;
*a = 20;
*b = 30;
c = 40;
cout << typeid(a).name() << endl;//int*
cout << typeid(b).name() << endl;//int*
cout << typeid(c).name() << endl;//int
system("pause");
return 0;
}
三、auto(C++11)
auto是一个类型修饰符,他会根据你给变量初始化的值的类型来决定自己是什么类型
auto定义的变量必须初始化,否则不知道auto是什么类型
auto可以连续定义变量,类型由第一个定义的变量决定,后续不能违背这个类型,但是可以定义对应类型的指针和引用
auto不能做函数的参数类型
auto不能参与数组的类型
C++11后,auto会失去原本的含义