在C/C++中,变量、函数和类都是大量存在的,这些变量、函数和类的名称如果都存在于全局作用域中,可能会导致很多冲突。使⽤命名空间的⽬的是对标识符的名称进⾏本地化,以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的
c语⾔项⽬类似下⾯程序这样的命名冲突是普遍存在的问题,C++引⼊namespace就是为了更好的解决这样的问题
#include
#include
int rand = 10;
int main() {
// 编译报错:error C2365: “rand”: 重定义;以前的定义是“函数”
printf("%d\n", rand);
return 0;
}
定义命名空间,需要使⽤到namespace关键字,后⾯跟命名空间的名字,然后接⼀对{}即可,{}中即为命名空间的成员。命名空间中可以定义变量/函数/类型等。
namespace本质是定义出⼀个域,这个域跟全局域各⾃独⽴,不同的域可以定义同名变量,所以下⾯的rand不在冲突了。
#include
#include
namespace abc
{
// 变量
int rand = 10;
// 函数
int Add(int a, int b) {
return a + b;
}
// 类型
struct Node {
int val;
struct Node* next;
}
}
int main() {
// 指定访问abc中的rand
// ::称为域作用限定符
printf("%d\n", abc::rand);
return 0;
}
C++中的域有函数局部域,全局域,命名空间域,类域;域影响的是编译时语法查找⼀个变量/函数/类型出处(声明或定义)的逻辑,所以有了域隔离,名字冲突就解决了。局部域和全局域除了会影响编译查找逻辑,还会影响变量的⽣命周期,命名空间域和类域不影响变量⽣命周期。
namespace只能定义在全局,当然他还可以嵌套定义。
// 嵌套定义
#include
namespace A
{
namespace B
{
int rand = 1;
int func(int a, int b) {
return a + b;
}
}
namespace C
{
int rand = 2;
int func(int a, int b) {
return a - b;
}
}
}
// 引用语法如下
int main() {
std::cout << "A::B::rand -> " << A::B::rand << std::endl;
std::cout << "A::C::rand -> " << A::C::rand << std::endl;
std::cout << "A::B::func(1,2) -> " << A::B::func(1, 2) << std::endl;
std::cout << "A::C::func(1,2) -> " << A::C::func(1, 2) << std::endl;
return 0;
}
编译查找⼀个变量的声明/定义时,默认只会在局部或者全局查找,不会到命名空间里面去查找。所以下面程序会编译报错。
#include
namespace A
{
int a = 0;
int b = 1;
}
int main() {
// 编译报错:error C2065 : “a”:未声明的标识符
printf("%d\n", a);
return 0;
}
所以我们要使⽤命名空间中定义的变量/函数,有三种⽅式:
指定命名空间访问,项⽬中推荐这种⽅式
#include
namespace A
{
int a = 0;
int b = 1;
}
int main() {
// 指定命名空间访问
printf("%d\n", A::a);
return 0;
}
using将命名空间中某个成员展开,项⽬中经常访问的不存在冲突的成员推荐这种⽅式
#include
namespace A
{
int a = 0;
int b = 1;
}
using A::b;
int main() {
printf("%d\n", A::a);
// using将命名空间中某个成员展开
printf("%d\n", b);
return 0;
}
展开命名空间中全部成员,项⽬不推荐,冲突⻛险很⼤,⽇常⼩练习程序为了⽅便推荐使⽤
#include
namespace A
{
int a = 0;
int b = 1;
}
using namespace A;
int main() {
// 展开命名空间中的全部成员
printf("%d\n", a);
printf("%d\n", b);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
using namespace std;
int main() {
int a = 0;
double b = 0.1;
char c = 'x';
// 展开std命名空间,实际项目开发中不建议使用
cout << a << " " << b << " " << c << endl;
std::cout << a << " " << b << " " << c << std::endl;
// 对比c/c++输入输出,后者可以自动识别变量类型
scanf("%d%lf", &a, &b);
printf("%d %lf\n", a, b);
cin >> a;
cin >> b >> c;
cout << a << endl;
cout << b << " " << c << endl;
// 由于c++输入输出的自动识别是通过重载实现的
// 为了便利,实际上做出了一些效率上的牺牲
// 在io需求⽐较⾼的地⽅,如部分⼤量输⼊的竞赛题中,加上以下3⾏代码
// 可以提高c++的IO效率
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}
#include
#include
using namespace std;
void Func(int a = 0)
{
cout << a << endl;
}
// 全缺省
void Func1(int a = 10, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
}
// 半缺省
void Func2(int a, int b = 10, int c = 20)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
}
int main()
{
Func(); // 没有传参时,使⽤参数的默认值
Func(10); // 传参时,使⽤指定的实参
Func1();
Func1(1);
Func1(1, 2);
Func1(1, 2, 3);
Func2(100);
Func2(100, 200);
Func2(100, 200, 300);
return 0;
}
C++⽀持在同⼀作⽤域中出现同名函数,但是要求这些同名函数的形参不同,可以是参数个数不同或者类型不同。这样C++函数调⽤就表现出了多态⾏为,使⽤更灵活。C语⾔是不⽀持同⼀作⽤域中出现同名函数的
#include
using namespace std;
// 1. 参数类型不同
int Add(int left, int right)
{
cout << "int Add(int left, int right)" << endl;
return left + right;
}
double Add(double left, double right)
{
cout << "double Add(double left, double right)" << endl;
return left + right;
}
// 2. 参数个数不同
void f()
{
cout << "f()" << endl;
}
void f(int a)
{
cout << "f(int a)" << endl;
}
// 3. 参数类型顺序不同,本质上也可以说是第1种吧
void f(int a, char b)
{
cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{
cout << "f(char b, int a)" << endl;
}
// 返回值不同不能作为重载条件,因为调⽤时也⽆法区分
/*
void fxx()
{}
int fxx()
{
return 0;
}
*/
// 下⾯两个函数构成重载
// 但是调⽤时,会报错,存在歧义,编译器不知道调⽤谁
void f1()
{
cout << "f1()" << endl;
}
void f1(int a = 10)
{
cout << "f1(int a)" << endl;
}
int main() {
Add(10, 20);
Add(10.1, 20.3);
f();
f(10);
f(10, 'a');
f('a', 10);
return 0;
}
引⽤不是新定义⼀个变量,⽽是给已存在变量取了⼀个别名,编译器不会为引⽤变量开辟内存空间,它和它引⽤的变量共⽤同⼀块内存空间
C++中为了避免引⼊太多的运算符,会复⽤C语⾔的⼀些符号,⽐如前⾯的<<和>>,这⾥引⽤也和取地址使⽤了同⼀个符号&,使⽤时注意区分就可以
#include
using namespace std;
int main()
{
int a = 0;
// 引用:b和c是a的别名
int& b = a;
int& c = a;
// 也可以给别名b取别名,d相当于还是a的别名
int& d = b;
++d;
// 这里取地址我们看到是一样的
cout << &a << endl;
cout << &b << endl;
cout << &c << endl;
cout << &d << endl;
return 0;
}
#include
using namespace std;
int main()
{
int a = 10;
// 编译报错:"ra":必须初始化引用
// int& ra;
int& b = a;
int c = 20;
// 这里并非让b引用c,因为c++引用不能改变指向
// 这里是一个赋值
b = c;
cout << &a << endl;
cout << &b << endl;
cout << &c << endl;
return 0;
}
#include
using namespace std;
typedef struct SeqList
{
int a[10];
int size;
}SLT;
// 使⽤C++引⽤替代指针传参,以简化程序
void SeqPushBack(SLT& sl, int x)
{}
typedef struct ListNode
{
int val;
struct ListNode* next;
}LTNode, * PNode;
// 指针变量也可以取别名,这⾥LTNode*& phead就是给指针变量取别名
// 这样就不需要⽤⼆级指针了,相对⽽⾔简化了程序
// void ListPushBack(LTNode** phead, int x)
// void ListPushBack(LTNode*& phead, int x)
void ListPushBack(PNode& phead, int x)
{
PNode newnode = (PNode)malloc(sizeof(LTNode));
newnode->val = x;
newnode->next = NULL;
if (phead == NULL)
{
phead = newnode;
}
else
{
// ...
}
}
int main()
{
PNode plist = NULL;
ListPushBack(plist, 1);
return 0;
}
int& rb = a*3; double d = 12.34; int& rd = d;
这样⼀些场景下a*3的和结果保存在⼀个临时对象中,int& rd = d
也是类似,在类型转换中会产⽣临时对象存储中间值,也就是时,rb和rd引⽤的都是临时对象,⽽C++规定临时对象具有常性,所以这⾥就触发了权限放⼤,必须要⽤常引⽤才可以#include
using namespace std;
int main()
{
const int a = 10;
// 编译报错:error C2440 : “初始化” :⽆法从“const int”转换为“int& ”
// 这⾥的引⽤是对a访问权限的放⼤
// int& ra = a;
// 这样才可以
const int& ra = a;
// 编译报错:error C3892 : “ra”:不能给常量赋值
// ra++;
// 这⾥的引⽤是对b访问权限的缩⼩
int b = 20;
const int& rb = b;
// 编译报错:error C3892 : “rb”:不能给常量赋值
// rb++;
int c = 10;
const int& ra = 30;
// 编译报错:"初始化":无法从"int"转换为"int &"
// int& rb = a * 3;
const int& rb = c * 3;
double d = 12.34;
// 编译报错:"初始化":无法从"double"转换为"int &"
// int& rd = d;
const int& rd = d;
return 0;
}
C++中指针和引⽤,在实践中相辅相成,功能有重叠性,但是各有⾃⼰的特点,互相不可替代
⽤inline修饰的函数叫做内联函数,编译时C++编译器会在调⽤的地⽅展开内联函数,这样调⽤内联函数就需要建⽴栈帧了,就可以提⾼效率
inline对于编译器⽽⾔只是⼀个建议,也就是说,你加了inline编译器也可以选择在调⽤的地⽅不展开,不同编译器关于inline什么情况展开各不相同,因为C++标准没有规定这个。inline适⽤于频繁调⽤的短⼩函数,对于递归函数,代码相对多⼀些的函数,加上inline也会被编译器忽略
C语⾔实现宏函数也会在预处理时替换展开,但是宏函数实现很复杂很容易出错的,且不⽅便调试,C++设计了inline⽬的就是替代C的宏函数
inline不建议声明和定义分离到两个⽂件,分离会导致链接错误。因为inline被展开,就没有函数地址,链接时会出现报错
vs编译器debug版本下⾯默认是不展开inline的,这样⽅便调试,debug版本想展开需要设置以下两个地⽅
#include
using namespace std;
inline int Add(int x, int y)
{
int ret = x + y;
ret += 1;
ret += 1;
ret += 1;
return ret;
}
int main() {
// 可以通过汇编观察程序是否展开
// 有call Add语句就是没有展开,没有就是展开了
int ret = Add(1, 2);
cout << Add(1, 2) * 5 << endl;
return 0;
}
#include
using namespace std;
// 实现一个ADD宏函数的常见问题
// #define ADD(int a, int b) return a + b;
// #define ADD(a, b) a + b;
// #define ADD(a, b) (a + b)
// 正确的宏实现
#define ADD(a, b) ((a) + (b))
// 可以思考:
// 为什么不能加分号?
// 为什么要加外面/里面的括号?
int main() {
int ret = ADD(1, 2);
cout << ADD(1, 2) << endl;
cout << ADD(1, 2) * 5 << endl;
int x = 1, y = 2;
ADD(x & y, x | y); // -> (x&y+x|y)
return 0;
}
// F.h
#include
using namespace std;
inline void f(int i);
// F.cpp
#include "F.h"
void f(int i)
{
cout << i << endl;
}
// main.cpp
#include "F.h"
int main() {
// 链接错误:⽆法解析的外部符号"void __cdecl f(int)" (? f@@YAXH@Z)
f(10);
return 0;
}
NULL实际是⼀个宏,在传统的C头⽂件(stddef.h)中,可以看到如下代码:
#ifdef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
#include
using namespace std;
void f(int x)
{
cout << "f(int x)" << endl;
}
void f(int* ptr)
{
cout << "f(int* ptr)" << endl;
}
int main() {
f(0);
// 本想通过f(NULL)调⽤指针版本的f(int*)函数
// 但是由于NULL被定义成0,调⽤了f(int x),因此与程序的初衷相悖
f(NULL);
f((int*)NULL);
// 编译报错:error C2665 : “f”: 2个重载中没有⼀个可以转换所有参数类型
// f((void*)NULL);
f(nullptr);
return 0;
}