【C++】1. C++基础知识

发布于:2025-07-30 ⋅ 阅读:(31) ⋅ 点赞:(0)

一、C++的第⼀个程序

C++兼容C语⾔绝⼤多数的语法,所以C语⾔实现的hello world依旧可以运⾏,C++中需要把定义⽂件代码后缀改为.cpp,vs编译器看到是.cpp就会调⽤C++编译器编译,linux下要⽤g++编译,不再是gcc。

#include<stdio.h>

int main()
{
	printf("hello world\n");

	return 0;
}

运行结果:

可以看到在C++的环境下依旧可以运行C语言的语法,展现了C++的兼容性。

当然C++有⼀套⾃⼰的输⼊输出,严格说C++版本的hello world应该是这样写的。

#include<iostream>
using namespace std;

int main()
{
	cout << "hello world" << endl;

	return 0;
}

运行结果:

二、命名空间

1、namespace的价值

在C/C++中,变量、函数和类都是⼤量存在的,这些变量、函数和类的名称将都存在于全局作⽤域中,可能会导致很多冲突。使⽤命名空间的⽬的是对标识符的名称进⾏本地化,以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的。

c语⾔项⽬类似下⾯程序这样的命名冲突是普遍存在的问题。

#include<stdio.h>
#include<stdlib.h>

int rand = 10;

int main()
{
	printf("%d", rand);
	return 0;
}

运行之后显示报错:

原因在于#include<stdlib.h>库中存在rand函数,而我们再去定义rand就会出现报错。

所以C++引⼊namespace就是为了更好的解决这样的问题。

2、namespace的定义

  • 定义命名空间,需要使⽤到namespace关键字,后⾯跟命名空间的名字,然后接⼀对{ }即可,{ }中即为命名空间的成员。命名空间中可以定义变量/函数/类型等。
#include<stdio.h>
#include<stdlib.h>

namespace zsy
{
	int rand = 10;

	int Add(int x, int y)
	{
		return x + y;
	}

	struct Node
	{
		struct Node* nedxt;
		int val;
	};
}

int main()
{
	printf("%d\n", zsy::rand);
	printf("%d\n", zsy::Add(1, 2));
	struct zsy::Node p1;

	return 0;
}

运行结果:

  • namespace本质是定义出⼀个域,这个域跟全局域各⾃独⽴,不同的域可以定义同名变量,所以下⾯的rand不再冲突了。

  • C++中域有函数局部域,全局域,命名空间域,类域。域影响的是编译时语法查找⼀个变量 / 函数 / 类型出处(声明或定义)的逻辑,所以有了域隔离,名字冲突就解决了。局部域和全局域除了会影响编译查找逻辑,还会影响变量的⽣命周期,命名空间域和类域不影响变量的⽣命周期。

  • namespace只能定义在全局,同时还可以嵌套定义。如下:

#include<stdio.h>
#include<stdlib.h>

namespace zsy
{

	namespace zs
	{
		int rand = 10;

		int Add(int x, int y)
		{
			return x + y;
		}
	}

	namespace hg
	{
		int rand = 20;

		int Add(int x, int y)
		{
			return x + y;
		}
	}
	
}

int main()
{
	printf("%d\n", zsy::zs::rand);
	printf("%d\n", zsy::zs::Add(1, 2));

	printf("%d\n", zsy::hg::rand);
	printf("%d\n", zsy::hg::Add(1, 2));

	return 0;
}

运行结果:

  • 项⽬⼯程中多⽂件中定义的同名namespace会认为是⼀个namespace,不会冲突。如下:

Stack.h:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

namespace zsy
{
	typedef int STDataType;
	typedef struct Stack
	{
		STDataType* a;
		int top;
		int capacity;
	}ST;

	void STInit(ST* ps, int n);
	void STDestroy(ST* ps);
	void STPush(ST* ps, STDataType x);
	void STPop(ST* ps);
	STDataType STTop(ST* ps);
	int STSize(ST* ps);
	bool STEmpty(ST* ps);
}

Stack.cpp:

#include"Stack.h"

namespace zsy
{
	void STInit(ST* ps, int n)
	{
		assert(ps);
		ps->a = (STDataType*)malloc(n * sizeof(STDataType));
		ps->top = 0;
		ps->capacity = n;
	}
	// 栈顶 
	void STPush(ST* ps, STDataType x)
	{
		assert(ps);
		// 满了, 扩容 
		if (ps->top == ps->capacity)
		{
			printf("扩容\n");
			int newcapacity = ps->capacity == 0 ? 4 : ps->capacity
				* 2;
			STDataType* tmp = (STDataType*)realloc(ps->a,
				newcapacity * sizeof(STDataType));
			if (tmp == NULL)
			{
				perror("realloc fail");
				return;
			}
			ps->a = tmp;
			ps->capacity = newcapacity;
		}
		ps->a[ps->top] = x;
		ps->top++;
	}

	//...
}

Test.cpp:

#include"Stack.h"
#include"Queue.h"

//全局定义
typedef struct Stack
{
	int a[10];
	int top;
}ST;

void STInit(ST* ps) {}
void STPush(ST* ps, int x) {}

int main()
{
    //调用全局的
	ST st1;
	STInit(&st1);
	printf("%d\n", sizeof(st1));//11*4

	STPush(&st1, 1);
	STPush(&st1, 2);


	//调用zsy的
	zsy::ST st2;
	zsy::STInit(&st2, 4);
	printf("%d\n", sizeof(st2));//4*4

	zsy::STPush(&st2, 1);
	zsy::STPush(&st2, 2);
	return 0;
}

运行结果:
在这里插入图片描述

  • C++标准库都放在⼀个叫 std(standard)的命名空间中。

3、命名空间的使⽤

  • 编译查找⼀个变量的声明/定义时,默认只会在局部或者全局查找,不会到命名空间⾥⾯去查找。
#include<stdio.h>

namespace zsy
{
	int a = 0;
	int b = 1;
}

int main()
{
	printf("%d\n", a);
	
	return 0;
}

编译报错:

  • 如果要使⽤命名空间中定义的变量/函数,有三种⽅式:

①:指定命名空间访问,项⽬中推荐这种⽅式。

#include<stdio.h>

namespace zsy
{
	int a = 0;
	int b = 1;
}

int main()
{
	printf("%d\n", zsy::a);//指定命名空间访问

	return 0;
}

运行结果:
在这里插入图片描述

②:使用using将命名空间中的某个成员展开,项⽬中经常访问的成员推荐这种⽅式。

#include<stdio.h>

namespace zsy
{
	int a = 0;
	int b = 1;
}

using zsy::a;//using将命名空间中某个成员展开
int main()
{
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", zsy::b);
	return 0;
}

运行结果:

③:展开命名空间中全部成员,项⽬不推荐,冲突⻛险很⼤,⽇常⼩练习程序为了⽅便推荐使⽤。

#include<stdio.h>

namespace zsy
{
	int a = 0;
	int b = 1;
}

using namespace zsy;//展开命名空间中全部成员
int main()
{
	printf("%d\n", a);
	printf("%d\n", b);

	return 0;
}

运行结果:
在这里插入图片描述

例如我们在平时练习中使用的下面这种方式,就是展开命名空间全部成员,可以直接使用C++库里面的各种函数。

#include<iostream>
using namespace std;

三、C++输⼊&输出

  • <iostream> 是 Input Output Stream 的缩写,是标准的输⼊、输出流库,定义了标准的输⼊、输出对象。

  • std::cin 是 istream 类的对象,它主要⾯向窄字符的标准输⼊流。

  • std::cout 是 ostream 类的对象,它主要⾯向窄字符的标准输出流。

  • std::endl 是⼀个函数,流插⼊输出时,相当于插⼊⼀个换⾏字符加刷新缓冲区。

  • <<是流插⼊运算符,>>是流提取运算符。(C语⾔还⽤这两个运算符做位运算左移/右移)

  • 使⽤C++输⼊输出更⽅便,不需要像printf/scanf输⼊输出时那样,需要⼿动指定格式,C++的输⼊输出可以⾃动识别变量类型,最重要的是C++的流能更好的⽀持⾃定义类型对象的输⼊输出。

  • cout/cin/endl等都属于C++标准库,C++标准库都放在⼀个叫std(standard)的命名空间中,所以要通过命名空间的使⽤⽅式去⽤他们。 ⼀般⽇常练习中我们可以using namespace std,实际项⽬开发中不建议using namespace std。

  • 这⾥我们没有包含<stdio.h>,也可以使⽤printf和scanf,在包含<iostream>就间接包含了。vs系列编译器是这样的,其他编译器可能会报错。

#include<iostream>
using namespace std;

int main()
{
	int a = 0;
	double b = 0.1;
	char c = 'x';

	cin >> a >> b >> c;
	cout << a << " " << b << " " << c << endl;

	printf("%d\n", a);

	return 0;
}

运行结果:

注意:在io需求比较高的地方,如部分大量输入的竞赛题中,加上以下3行代码可以提⾼C++IO效率。

ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);

四、缺省参数

  • 缺省参数是声明或定义函数时为函数的参数指定⼀个缺省值。在调⽤该函数时,如果没有指定实参则采⽤该形参的缺省值,否则使⽤指定的实参,缺省参数分为全缺省和半缺省参数。(有些地⽅把缺省参数也叫默认参数)
#include <iostream>
using namespace std;

void Fun(int n = 10)
{
	cout << n << endl;
}

int main()
{
	Fun();//使用缺省值
	Fun(20);//使用指定实参
	 
	return 0;
}

运行结果:
在这里插入图片描述

  • 全缺省就是全部形参给缺省值,半缺省就是部分形参给缺省值。C++规定半缺省参数必须从右往左依次连续缺省,不能间隔跳跃给缺省值。

  • 带缺省参数的函数调⽤,C++规定必须从左到右依次给实参,不能跳跃给实参。

#include <iostream>
using namespace std;

//全缺省
void Fun1(int a = 10, int b = 20, int c = 30)
{
	cout << a << " " << b << " " << c << endl;
}

//半缺省
void Fun2(int a, int b = 20, int c = 30)
{
	cout << a << " " << b << " " << c << endl;
}

int main()
{
	Fun1();
	Fun1(1);
	Fun1(1,2);
	Fun1(1,2,3);
	cout << endl;
	Fun2(1);
	Fun2(1,2);
	Fun2(1,2,3);

	return 0;
}

运行结果:

  • 函数声明和定义分离时,缺省参数不能在函数声明和定义中同时出现,只能在函数声明时给缺省值。
// Stack.h
#include<iostream>
#include<assert.h>

using namespace std;

typedef int SLDataType;
typedef struct Stack
{
	SLDataType* a;
	int top;
	int capacity;
}ST;

//函数声明时给缺省值
void STInit(ST* ps, int n = 4);

// Stack.cpp
#include"Stack.h"

void STInit(ST* ps, int n)
{
	assert(ps && n > 0);
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * n);
	ps->top = 0;
	ps->capacity = n;
}

// test.cpp
#include"Stack.h"

int main()
{
	ST s1;
	STInit(&s1);//初始化为缺省值

	ST s2;
	STInit(&s2, 1000);//初始化为1000个空间

	return 0;
}

调试观察:

因此当我们知道要插入多少个数据时,就可以通过使用缺省参数的这种方式直接初始化为1000个空间的大小。

五、函数重载

  • C++⽀持在同⼀作⽤域中出现同名函数,但是要求这些同名函数的形参不同,可以是参数个数不同或者类型不同。这样C++函数调⽤就表现出了多态⾏为,使⽤更灵活。C语⾔中是不⽀持同⼀作⽤域中出现同名函数的。
#include<iostream>
using namespace std;

//参数类型不同
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;
}

//参数个数不同
void Fun()
{
	cout << "Fun( ) " << endl;
}

void Fun(int a)
{
	cout << "Fun(int a)" << endl;
}

//参数类型顺序不同
void Fun(int a, char b)
{
	cout << "Fun(int a,char b)" << endl;
}

void Fun(char b, int a)
{
	cout << "Fun(char b,int a)" << endl;
}


int main()
{
	Add(10, 20);
	Add(10.1, 20.1);

	Fun();
	Fun(1);
	Fun(1,'a');
	Fun('a', 1);
	return 0;
}

运行结果:

  • 返回值不同不能作为重载条件,因为调用时无法区分。
void Fun()
{
	cout << "Fun( )" << endl;
}

int Fun(int a)
{
	cout << "Fun(int a)" << endl;
	return a;
}

int main()
{
	Fun();
	Fun(1);
	return 0;
}

运行结果:

上面两个函数在这里构成了重载,判断依据是他们的参数类型与参数个数不同,而不是因为返回值不同。

  • 下面这两个函数构成重载,但是调用Fun()时,编译器会报错。
void Fun()
{
	cout << " Fun( )" << endl;
}

void Fun(int a = 10)//缺省参数
{
	cout << "Fun(int a=10)" << endl;
}

int main()
{
	Fun();

	return 0;
}

编译报错:

因为这两个函数都能匹配,编译器无法确定选择哪一个。

六、引⽤

1、引⽤的概念和定义

引⽤不是新定义⼀个变量,⽽是给已存在变量取了⼀个别名,编译器不会为引⽤变量开辟内存空间,它和它引⽤的变量共⽤同⼀块内存空间。⽐如:⽔壶传中李逵,宋江叫其"铁⽜",江湖上⼈称"⿊旋⻛"。

使用方式:
类型& 引⽤别名 = 引⽤对象;

C++中为了避免引⼊太多的运算符,会复⽤C语⾔的⼀些符号,⽐如前⾯的<< 和 >>,这⾥引⽤也和取地址使⽤了同⼀个符号&。

#include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int& b = a;//给a取别名b,b改变a也会跟着改变
	b = 20;

	cout << a << endl;
	cout << b << endl;

	return 0;
}

运行结果:

2、引⽤的特性

  • 引⽤在定义时必须初始化
#include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int& b;

	return 0;
}

编译报错:

  • ⼀个变量可以有多个引⽤
#include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int& b = a;//a的别名,指向a
	int& c = a;
	int& d = c;//别名的别名 还指向a
	++d;

	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
	cout << d << endl;

	return 0;
}

运行结果:

  • 引⽤⼀旦引⽤⼀个实体,再不能引⽤其他实体
#include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int b = 20;

	int& c = a;
	int& c = b;

	return 0;
}

编译报错:

  • C++中引⽤不能改变指向
int main()
{
	int a = 10;
	int& b = a;

	int c = 20;
	b = c;//这里是将c赋值给b,b依旧指向a

	cout << a << endl;
	cout << b << endl;
	cout << c << endl;

	return 0;
}

运行结果:

3、引⽤的使⽤

  • 引⽤在实践中主要是用于引⽤传参和引⽤做返回值中减少拷⻉提⾼效率、改变引⽤对象时同时改变被引⽤对象。

  • 引⽤传参跟指针传参功能是类似的,引⽤传参相对更⽅便⼀些。

#include<iostream>
using namespace std;

void Swap(int& rx, int& ry)
{
	int tmp = rx;
	rx = ry;
	ry = tmp;
}

int main()
{
	int x = 10, y = 20;
	cout << x << " " << y << endl;
	
	Swap(x, y);
	cout << x << " " << y << endl;

	return 0;
}
  • 引⽤和指针在实践中相辅相成,功能有重叠性,但是各有特点,互相不可替代。C++的引⽤跟其他语⾔的引⽤(如Java)是有很⼤的区别的,除了⽤法外最⼤的点,就是C++引⽤定义后不能改变指向,Java的引⽤可以改变指向。

例1:
指针实现栈方法:

#include<assert.h>

typedef int SLDataType;
typedef struct Stack
{
	SLDataType* a;
	int top;
	int capacity;
}ST;

void STInit(ST* ps, int n = 4)
{
	assert(ps && n > 0);
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * n);
	ps->top = 0;
	ps->capacity = n;
}

void STPush(ST* ps, SLDataType x)
{
	assert(ps);

	//检查空间
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity*sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
          return;
		}

		ps->a = tmp;
		ps->capacity = newcapacity;
	}

	//插入数据
	ps->a[ps->top] = x;
	ps->top++;
}

int STTop(ST* ps)
{
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

int main()
{
	ST st1;
	STInit(&st1);
	STPush(&st1, 1);
	STPush(&st1, 2);
	cout << STTop(&st1) << endl;

	return 0;
}

运行结果:
在这里插入图片描述

引用实现栈方法:

#include<iostream>
#include<assert.h>
using namespace std;

typedef int SLDataType;
typedef struct Stack
{
	SLDataType* a;
	int top;
	int capacity;
}ST;

void STInit(ST& rs, int n = 4)
{
	rs.a = (SLDataType*)malloc(sizeof(SLDataType) * n);
	rs.top = 0;
	rs.capacity = n;
}

void STPush(ST& rs, SLDataType x)
{
	if (rs.top == rs.capacity)
	{
		int newcapacity = rs.capacity == 0 ? 4 : rs.capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(rs.a, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}

		rs.a = tmp;
		rs.capacity = newcapacity;
	}

	//插入数据
	rs.a[rs.top] = x;
	rs.top++;
}

int& STTop(ST& rs)
{
	assert(rs.top > 0);
	return rs.a[rs.top - 1];
}

int main()
{
	ST st1;
	STInit(st1);
	STPush(st1, 1);
	STPush(st1, 2);
	cout << STTop(st1) << endl;

	STTop(st1) = 10;//修改栈顶元素
	cout << STTop(st1) << endl;

	return 0;
}

运行结果:
在这里插入图片描述

注意通过引用,可以直接修改栈顶的元素,如图所示:
在这里插入图片描述

例2:

typedef struct ListNode
{
	int val;
	struct ListNode* next;
}LTNode, * PNode;

//void ListPushBack(LTNode** phead, int x)
//void ListPushBack(LTNode*& phead, int x)
// 指针变量也可以取别名,这⾥LTNode*& phead就是给指针变量取别名 
// 这样就不需要用二级指针了,相对而言简化了程序 

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;
}

可以看到在编写链表时就可以不再使用二级指针了,在C++中可以使用引用,相对而言是简化了程序的。

  • 在这里还要注意一种情况:返回局部变量的引用。
#include<iostream>
using namespace std;

int& f()
{
	int a = 0;
	return a;
}

int main()
{
	cout << f() << endl;

	return 0;
}

在这里会报出警告:

由于a是在函数内部创建的局部变量,它存储在栈上,当函数执行完毕,局部变量a就会被销毁,占用的空间也会被释放。而此时却返回了这个局部变量的引用,这种引用被称为 “悬空引用”,使用这样的引用会导致未定义行为,比如可能输出随机值,或者使程序崩溃。

4、const引⽤

  • 可以引⽤⼀个const对象,但是必须⽤const引⽤。const引⽤也可以引⽤普通对象,因为对象的访问权限在引⽤过程中可以缩⼩,但是不能放⼤。

引用const对象:

int main()
{
	const int a = 10;
	const int& ra = a;//针对const对象需要用const引用

	return 0;
}

引用普通对象:

int main()
{
	//这里的引用是对a访问权限的放大 
	const int a = 10;
	int& ra = a;//err 不能放大权限

	return 0;
}

编译报错:
在这里插入图片描述
可以看到权限是不能放大的,只能缩小。

int main()
{
	//这里的引用是对a访问权限的缩小
	int b = 10;
	const int& rb = b;

	//const修饰对象不可修改
	b++;
	//rb++; //err

	cout << b << endl;
	cout << rb << endl;
	return 0;
}

运行结果:
在这里插入图片描述

  • 还要注意,当某些值存放在临时对象时,也需要使用const引用。
  • 所谓临时对象就是编译器需要⼀个空间暂存表达式的求值结果时临时创建的⼀个未命名的对象,C++中把这个未命名对象叫做临时对象。

场景1:

int a = 10;
//int& rb = a*3;  //err
const int& rb = a * 3;

在这个场景下a*3运算后的值会保存在⼀个临时对象中,C++规定临时对象具有常性,因此针对临时对象引用时需要使用常引用(const)。

场景2:

double d = 12.34; 
//int& rd = d;    //err
const int& rd = d;

同理,double类型的值在这里转变为int类型,会发生整型提升,整型提升后的值会保存在临时对象中,针对临时对象引用需要使用常引用。

5、指针和引⽤的关系

C++中指针和引⽤就像两个性格迥异的亲兄弟,指针是哥哥,引⽤是弟弟,在实践中他们相辅相成,功能有重叠性,但是各有⾃⼰的特点,互相不可替代。

  • 语法概念上引⽤是⼀个变量的取别名,不开空间;指针是存储⼀个变量的地址,要开空间。

  • 引⽤在定义时必须初始化;指针建议初始化,但是语法上不是必须的。

  • 引⽤在初始化时引⽤⼀个对象后,就不能再引⽤其他对象;⽽指针可以再不断地改变指向对象。

  • 引⽤可以直接访问指向对象;指针需要解引⽤才是访问指向对象。

  • sizeof中含义不同,引⽤结果为引⽤类型的⼤⼩,但指针始终是地址空间所占字节个数(32位平台下占4个字节,64位下是8byte)

  • 指针很容易出现空指针和野指针的问题,引⽤很少出现,引⽤使⽤起来相对更安全⼀些。

int main()
{
	int a = 0;

	int* p = &a;
	*p = 1;
	
	int& ra = a;
	ra = 2;

	return 0;
}

调试观察反汇编代码:

发现他们的底层其实是一样,但各自使用达成的效果是大大不同的。

七、内联函数(inline)

  • ⽤inline修饰的函数叫做内联函数,编译时C++编译器会在调⽤的地⽅直接展开内联函数,这样调⽤内联函数就不需要建⽴栈帧了,就可以提⾼效率。
#include<iostream>
using namespace std;

inline int Add(int x, int y)
{
	int ret = x + y;
	ret += 1;
	return ret;
}

int main()
{
	cout << Add(1, 2) * 5 << endl;
	
	return 0;
}

调试观察反汇编代码:

这里没有call Add语句,说明已经直接展开了。

  • inline对于编译器⽽⾔只是⼀个建议,也就是说,你加了inline编译器也可以选择在调⽤的地⽅不展开,不同编译器关于inline什么情况展开各不相同,因为C++标准没有规定这个。inline适⽤于频繁调⽤的短⼩函数,对于递归函数以及代码相对多⼀些的函数,加上inline也会被编译器忽略。

  • C语⾔实现宏函数也会在预处理时替换展开,但是宏函数实现很复杂很容易出错,且不⽅便调试,C++设计了inline⽬的就是替代C的宏函数。

#include<iostream>
using namespace std;
// 实现⼀个ADD宏函数的常见问题 
//#define ADD(int a, int b) return a + b; //err
//#define ADD(a, b) a + b; //err
//#define ADD(a, b) (a + b)//err
// 正确的宏实现 
#define ADD(a, b) ((a) + (b))
// 1.为什么不能加分号? 
// 2.为什么要加外面的括号? 
// 3.为什么要加里面的括号? 
int main()
{
	cout << ADD(1, 2) << endl;//1.
	cout << ADD(1, 2) * 5 << endl;//2.
	cout << ADD(1 & 2, 1 | 2) << endl; //  (1&2+1|2) +的优先级更高  //3.

	return 0;
}
  • vs编译器 debug版本下⾯默认是不展开inline的,这样⽅便调试,debug版本想展开需要设置⼀下以下两个地⽅。
  • inline不建议声明和定义分离到两个⽂件,分离会导致链接错误。因为inline被展开,就没有函数地址,链接时会出现报错。

八、nullptr关键字

NULL实际是⼀个宏,在传统的C头⽂件(stddef.h)中,可以看到如下代码:

#ifndef NULL
    #ifdef __cplusplus
        #define NULL 0
    #else
        #define NULL ((void *)0)
    #endif
#endif

观察如下代码:

#include<iostream>
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((void*)NULL);//err

	return 0;
}

运行结果:
在这里插入图片描述

发现传参数NULL的时候并没有调用第二个函数,那么这是为什么呢?

  • 在C++中NULL可能被定义为字⾯常量0,或者C中被定义为⽆类型指针(void*)的常量。不论采取何种定义,在使⽤空值的指针时,都不可避免的会遇到⼀些⿇烦,本想通过f(NULL)调⽤指针版本的f(int*)函数,但是由于NULL被定义成0,调⽤了f(int x),因此与程序的初衷相悖。f((void*)NULL);调⽤会报错。

  • 因此C++11中引⼊nullptr,nullptr是⼀个特殊的关键字,nullptr是⼀种特殊类型的字⾯量,它可以转换成任意其他类型的指针类型。使⽤nullptr定义空指针可以避免类型转换的问题,因为nullptr只能被隐式地转换为指针类型,⽽不能被转换为整数类型。如下:

#include<iostream>
using namespace std;

void f(int x)
{
	cout << "f(int x)" << endl;
}

void f(int* ptr)
{
	cout << "f(int* ptr)" << endl;
}

int main()
{
	f(nullptr);

	return 0;
}

运行结果:


网站公告

今日签到

点亮在社区的每一天
去签到