纪念 229头像
关注
C++入门基础下封面图

C++入门基础下

在这里插入图片描述

༺ 个人主页 · 纪念229 ༻

🏠我的博客主页🏠

༒专栏目录:《数据结构》༒

༒专栏目录:《算法》༒

༒专栏目录:《C++》༒

༒专栏目录:《MySQL数据库》༒

༒专栏目录:《前端开发》༒

༒其它有趣的计算机知识༒

༺世上本没有路,走的人多了自然就有了༻


本篇文章基于上篇补上引用的知识点以及缺省参数、 函数重载、引用应用的具体代码讲解,希望对你有所帮助



1.引用代码讲解

代码讲解我就讲引用的知识点其它的我就不解释了

代码展示

void Swap(int& rx, int& ry)
{
int tmp = rx;
rx = ry;
ry = tmp;
}
int main()
{
int x = 0, y = 1;
cout << x <<" " << y << endl;
Swap(x, y);
cout << x << " " << y << endl;
return 0;
}

#include<iostream>
using namespace std;
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void STInit(ST& rs, int n = 4)
{
rs.a = (STDataType*)malloc(n * sizeof(STDataType));
rs.top = 0;
rs.capacity = n;
 }
/
/ 栈顶
void STPush(ST& rs, STDataType x)
{
assert(ps);
// 满了, 扩容
if (rs.top == rs.capacity)
{
printf("扩容\n");
int newcapacity = rs.capacity == 0 ? 4 : rs.capacity * 2;
STDataType* tmp = (STDataType*)realloc(rs.a, newcapacity *
sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
} r
s.a = tmp;
rs.capacity = newcapacity;
} r
s.a[rs.top] = x;
rs.top++;
} /
/ int STTop(ST& rs)
int& STTop(ST& rs)
{
assert(rs.top > 0);
return rs.a[rs.top];
} i
nt main()
{
// 调⽤全局的
ST st1;
STInit(st1);
STPush(st1, 1);
STPush(st1, 2);
cout << STTop(st1) << endl;
STTop(st1) += 10;
cout << STTop(st1) << endl;
return 0;
}

#include<iostream>
using namespace std;
typedef struct SeqList
{
int a[10];
int size;
}SLT;
// ⼀些主要⽤C代码实现版本数据结构教材中,使⽤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;
} e
lse
{
//...
}
}
int main()
{
PNode plist = NULL;
ListPushBack(plist, 1);
return 0;
}

具体讲解

**void Swap(int& rx, int& ry)
{
int tmp = rx;
rx = ry;
ry = tmp;
} i
nt main()
{
int x = 0, y = 1;
cout << x <<" " << y << endl;
Swap(x, y);
cout << x << " " << y << endl;
return 0;
}**

Swap(int& rx, int& ry)
这里形参被引用了即我传入的实参和形参的数据内容和指针相同
即交换rx 与 ry 实际上就是交换 x 与 y

#include<iostream>
using namespace std;
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void STInit(ST& rs, int n = 4)
{
rs.a = (STDataType*)malloc(n * sizeof(STDataType));
rs.top = 0;
rs.capacity = n;
}

/ 栈顶
void STPush(ST& rs, STDataType x)
{
assert(ps);
// 满了, 扩容
if (rs.top == rs.capacity)
{
printf("扩容\n");
int newcapacity = rs.capacity == 0 ? 4 : rs.capacity * 2;
STDataType* tmp = (STDataType*)realloc(rs.a, newcapacity *
sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
} r
s.a = tmp;
rs.capacity = newcapacity;
} r
s.a[rs.top] = x;
rs.top++;
} /
/ int STTop(ST& rs)
int& STTop(ST& rs)
{
assert(rs.top > 0);
return rs.a[rs.top];
} i
nt main()
{
// 调⽤全局的
ST st1;
STInit(st1);
STPush(st1, 1);
STPush(st1, 2);
cout << STTop(st1) << endl;
STTop(st1) += 10;
cout << STTop(st1) << endl;
return 0;
}



这里的初始化,入栈,取栈顶元素中的形参引用其实没啥用,就是用引用代替传指针优化代码可读性

```c
/
/ int STTop(ST& rs)
int& STTop(ST& rs)
{
assert(rs.top > 0);
return rs.a[rs.top];
}

STTop(st1) += 10;

这里的STTop函数返回值有用返回的不是临时变量而是栈顶元素中的一个在这种情况下
STTop(st1) += 10;
栈中元素是会发生改变的

这里我们有个疑问?
但是这这种写法能接类型加引用的变量名吗?它返回的不就是类型引用的东西吗?
✅ 可以!

函数返回 int& ,调用时,接收的变量也可以定义成 int&

int& refTop = STTop(st);

其它的我就不解释了

2.引用知识点的补充

2.1const引⽤

• 可以引⽤⼀个const对象,但是必须⽤const引⽤。const引⽤也可以引⽤普通对象,因为对象的访问权限在引⽤过程中可以缩⼩,但是不能放⼤。
• 不需要注意的是类似 int& rb = a3; double d = 12.34; int& rd = d; 这样⼀些场景下a3的和结果保存在⼀个临时对象中, int& rd = d 也是类似,在类型转换中会产⽣临时对象存储中间值,也就是时,rb和rd引⽤的都是临时对象,⽽C++规定临时对象具有常性,所以这⾥就触发了权限放⼤,必须要⽤常引⽤才可以。
• 所谓临时对象就是编译器需要⼀个空间暂存表达式的求值结果时临时创建的⼀个未命名的对象,C++中把这个未命名对象叫做临时对象。

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++;
return 0;
}

#include<iostream>
using namespace std;
int main()
{
int a = 10;
const int& ra = 30;
// 编译报错: “初始化”: ⽆法从“int”转换为“int &”
// int& rb = a * 3;
const int& rb = a*3;
double d = 12.34;
// 编译报错:“初始化”: ⽆法从“double”转换为“int &”
// int& rd = d;
const int& rd = d;
return 0;
}

2.2指针和引⽤的关系

C++中指针和引⽤就像两个性格迥异的亲兄弟,指针是哥哥,引⽤是弟弟,在实践中他们相辅相成,功能有重叠性,但是各有⾃⼰的特点,互相不可替代。
• 语法概念上引⽤是⼀个变量的取别名不开空间,指针是存储⼀个变量地址,要开空间。
• 引⽤在定义时必须初始化,指针建议初始化,但是语法上不是必须的。
• 引⽤在初始化时引⽤⼀个对象后,就不能再引⽤其他对象;⽽指针可以在不断地改变指向对象。
• 引⽤可以直接访问指向对象,指针需要解引⽤才是访问指向对象。
• sizeof中含义不同,引⽤结果为引⽤类型的⼤⼩,但指针始终是地址空间所占字节个数(32位平台下占4个字节,64位下是8byte)
• 指针很容易出现空指针和野指针的问题,引⽤很少出现,引⽤使⽤起来相对更安全⼀些。

3.inline

• ⽤inline修饰的函数叫做内联函数,编译时C++编译器会在调⽤的地⽅展开内联函数,这样调⽤内联函数就需要建⽴栈帧了,就可以提⾼效率。
• inline对于编译器⽽⾔只是⼀个建议,也就是说,你加了inline编译器也可以选择在调⽤的地⽅不展开,不同编译器关于inline什么情况展开各不相同,因为C++标准没有规定这个。inline适⽤于频繁调⽤的短⼩函数,对于递归函数,代码相对多⼀些的函数,加上inline也会被编译器忽略。
• C语⾔实现宏函数也会在预处理时替换展开,但是宏函数实现很复杂很容易出错的,且不⽅便调试,C++设计了inline⽬的就是替代C的宏函数。
• vs编译器 debug版本下⾯默认是不展开inline的,这样⽅便调试,debug版本想展开需要设置⼀下以下两个地⽅。
• inline不建议声明和定义分离到两个⽂件,分离会导致链接错误。为inline被展开,就没有函数地址,链接时会出现报错。
在这里插入图片描述

在这里插入图片描述

代码展示


#include<iostream>
using namespace std;
inline int Add(int x, int y)
{
int ret = x + y;
ret += 1;
ret += 1;
ret += 1;
return ret;
} i
nt main()
{
// 可以通过汇编观察程序是否展开
// 有call Add语句就是没有展开,没有就是展开了
int ret = Add(1, 2);
cout << Add(1, 2) * 5 << endl;
return 0;
}

#include<iostream>
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 <iostream>
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;
}

4.nullptr

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

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

• 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;
} v
oid f(int* ptr)
{
cout << "f(int* ptr)" << endl;
} i
nt 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;
}

5.C++入门基础代码讲解

代码库在gitee上以下是链接
https://gitee.com/the-dawn-and-dusk-glow/the-evolution-of-c-code

有些代码我没讲到,我的代码里有详细注释

代码展示
Stack.h

#pragma once

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


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

  void STInit(ST* ps, int n = 4);//缺省参数只在函数声明用
  void STDestroy(ST* ps);
  void STPush(ST* ps, STDataType x);
  void STPop(ST* ps);
  //是声明不是使用不能用&ps要用ST* ps
  STDataType STTop(ST* ps);
  int STSize(ST* ps);
  bool STEmpt(ST* ps);

Stack.cpp

#define _CRT_SECURE_NO_WARNINGS 1


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

  void STInit(ST* ps, int n)
  {
    assert(ps);
    ps->a = (STDataType*)malloc(n * sizeof(STDataType));
    ps->top = 0;
    ps->capacity = n;
  }
  //入栈
  //要用realloc栈满要扩容原来的数据不能消失
  void STPush(ST* ps, STDataType x)
  {
    assert(ps);
    //若是栈满就扩容
    if (ps->top == ps->capacity)
    {
      printf("\n");
      int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
      //realloc有两个参数reallco(所在地址,要求总字节个数)
      STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
      if (tmp == NULL)
      {
        perror("realloc fail");
        exit(1);
      }
      ps->a = tmp;
      //最后要将newcapacity更新到ps->capacity
      ps->capacity = newcapacity;
    }
    ps->a[ps->top] = x;
    ps->top++;
  }
  //...

Test.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;

////全缺省
//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 = 20, int c = 30)
//{
//  cout << "a= " << a << endl;
//  cout << "b= " << b << endl;
//  cout << "c= " << c << endl << endl;
//
//}

//#include"Stack.h"
//
//int main()
//{
//  //Func1();
//  //Func1(1);
//  //Func1(1,2);
//  //Func1(1,2,3);
//
//  ////全缺省参数才可以使使用自定义函数时不带实参
//  ////设计设置默认形参从右到左以及传实参从左到右目的是让
//  ////未默认设置的形参有传的实参用
//  //Func2(1);
//  //Func2(1,2);
//  //Func2(1,2,3);
//
//  ////确定要插入100个值
//  //ST st1;
//  ////初始化直接开好空间,避免扩容,提高效率
//  //STInit(&st1, 100);
//  //for (size_t i = 0; i < 100; i++)
//  //{
//  //  STPush(&st1, i);
//  //}
//
//  ////不确定要插入多少个值
//  //ST st2;
//  //STInit(&st2);
//  //STPush(&st2, 1);
//  //STPush(&st2, 2);
//
//  //开空间分为原地开和异地开 
//  //当尝试原地开后面没有足够的空间就会异地开
//
//  STDataType* p1 = (STDataType*)malloc(4 * sizeof(STDataType));
//  //realloc(要求指针, 所需空间字节大小)
//  STDataType* p2 = (STDataType*)realloc(p1, 16 * sizeof(STDataType));
//  cout << p1 << endl;
//  cout << p2 << endl;
//  return 0;
//}

//函数重载的应用

////1.参数类型不同
////A
//void swap(int* p1, int* p2)
//{}
//
////B
//void swap(double* p1, double* p2)
//{}
//
//void swap(char* p1, char* p2)
//{}

////2.参数个数不同
//void f()
//{
//	cout << "f()" << endl;
//}
//
//void f(int a)
//{
//	cout << "f(int a)" <<endl;
//}
//
//
////3、参数类型顺序不同
//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;
//}

//下面两个参数构成重载
//f1()但是调用时, 会报错, 存在歧义, 编译器不知道调用谁

//void f1()
//{
//	cout << "f1()" << endl;
//}
//
//void f1(int a = 10)
//{
//  cout << "f1(int a)" << endl;
//}

//void fxx()
//{}
//
//int fxx()
//{
//	return 0;
//}




//int main()
//{
//  int x1 = 1, x2 = 2;
//	double d1 = 1.1, d2 = 2.2;
//	//swap(&x1, &x2);
//	//swap(&d1, &d2);
//
//
//	//f1(1);
//	//f1();
//
//
//	//fxx();
//
//	return 0;
//}

////////////////////////////////////

//引用

//int main()
//{
//  int a = 10;
//  int& b = a;
//
//  cout << &a << endl;
//  cout << &b << endl;
//
//  b++;
//
//  cout << a << endl;
//  cout << b <<endl;
//
//  int& c = a; 
//  int& d = b;
//
//  cout << &a << endl;
//  cout << &b << endl;
//  cout << &c << endl;
//  cout << &d << endl;
//
//  int x = 20;
//  //引用不会改变指向, 所以这里是把x赋值修改c
//  c = x;
//  
//
//  cout << &a << endl;
//  cout << &b << endl;
//  cout << &c << endl;
//  cout << &d << endl;
//
//  //mian函数最后补return 0;
//  return 0;
//}


//void swap(int* p1, int* p2)
//{
//  int tmp = *p1;
//	*p1 = *p2;
//	*p2 = tmp;
//
//}
//
////引用功能1:做函数形参,修改形参影响实参
//void swap(int& p1, int& p2)
//{
//	int tmp = p1;
//	p1 = p2;
//	p2 = tmp;
//}
//
//struct A
//{
//	int a[100];
//	int b;
//	//....
//};
//
//void func(struct A* aa)
//{}
//
////引用功能2:做函数形参, 减少拷贝,提高效率
//void func(struct A& aa)
//{}
//
//
////void swap(int p1, int p2)
////{
////  int tmp = p1;
////	p1 = p2;
////	p2 = tmp;
////
////
////}
//
//
//
//int main()
//{
//  int x = 1, y = 2;
//	swap(&x, &y);
//	swap(x, y);
//
//	struct A a;
//	func(&a);
//	func(a);
//
//	return 0;
//}

int main()
{
int x = 1,y = 2;
int* px = &x,* py = &y;
int*& rpx = px;

cout << px << endl;
cout << py <<endl;
	return 0;
}

具体讲解

缺省参数

////全缺省
//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 = 20, int c = 30)
//{
//  cout << "a= " << a << endl;
//  cout << "b= " << b << endl;
//  cout << "c= " << c << endl << endl;
//
//}
//
//int main()
//{
//  //Func1();
//  //Func1(1);
//  //Func1(1,2);
//  //Func1(1,2,3);
//
//  ////全缺省参数才可以使使用自定义函数时不带实参
//  ////设计设置默认形参从右到左以及传实参从左到右目的是让
//  ////未默认设置的形参有传的实参用
//  //Func2(1);
//  //Func2(1,2);
//  //Func2(1,2,3);
//
//  ////确定要插入100个值
//  //ST st1;
//  ////初始化直接开好空间,避免扩容,提高效率
//  //STInit(&st1, 100);
//  //for (size_t i = 0; i < 100; i++)
//  //{
//  //  STPush(&st1, i);
//  //}
//
//  ////不确定要插入多少个值
//  //ST st2;
//  //STInit(&st2);
//  //STPush(&st2, 1);
//  //STPush(&st2, 2);
//
//  //开空间分为原地开和异地开 
//  //当尝试原地开后面没有足够的空间就会异地开
//
//  STDataType* p1 = (STDataType*)malloc(4 * sizeof(STDataType));
//  //realloc(要求指针, 所需空间字节大小)
//  STDataType* p2 = (STDataType*)realloc(p1, 16 * sizeof(STDataType));
//  cout << p1 << endl;
//  cout << p2 << endl;
//  return 0;
//}

这里还是讲的是缺省参数
全缺省参数自定义函数用的时候可以不传实参

设置默认形参也是从右往左设置

void STInit(ST* ps, int n = 4);//缺省参数只在函数声明用

这里的栈初始化也是半缺省参数

//  STDataType* p1 = (STDataType*)malloc(4 * sizeof(STDataType));
//  //realloc(要求指针, 所需空间字节大小)
//  STDataType* p2 = (STDataType*)realloc(p1, 16 * sizeof(STDataType));

这里讲一下这个realloc的用法

当我们mlloc一个指针空间时入栈空间不够要扩容(realloc)
有两种原地扩容和异地扩容
当realloc在原地扩容到其空间不够时就到其它地方扩容

malloc有一个形参realloc有两个形参realloc(目标指针, 扩容所需字节个数)

函数重载

//函数重载的应用

////1.参数类型不同
////A
//void swap(int* p1, int* p2)
//{}
//
////B
//void swap(double* p1, double* p2)
//{}
//
//void swap(char* p1, char* p2)
//{}

////2.参数个数不同
//void f()
//{
//	cout << "f()" << endl;
//}
//
//void f(int a)
//{
//	cout << "f(int a)" <<endl;
//}
//
//
////3、参数类型顺序不同
//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;
//}

//下面两个参数构成重载
//f1()但是调用时, 会报错, 存在歧义, 编译器不知道调用谁

//void f1()
//{
//	cout << "f1()" << endl;
//}
//
//void f1(int a = 10)
//{
//  cout << "f1(int a)" << endl;
//}

//void fxx()
//{}
//
//int fxx()
//{
//	return 0;
//}
//int main()
//{
//  int x1 = 1, x2 = 2;
//	double d1 = 1.1, d2 = 2.2;
//	//swap(&x1, &x2);
//	//swap(&d1, &d2);
//
//
//	//f1(1);
//	//f1();
//
//
//	//fxx();
//
//	return 0;
//}

以上是具体列子
函数重载就三点要求
参数类型不同,参数个数不同,参数类型顺序不同

//下面两个参数构成重载
//f1()但是调用时, 会报错, 存在歧义, 编译器不知道调用谁

//void f1()
//{
//	cout << "f1()" << endl;
//}
//
//void f1(int a = 10)
//{
//  cout << "f1(int a)" << endl;
//}

这里报错原因是缺省参数的问题因为是全缺省参数没传实参时会触发缺省参数系统就不知道调用第一个还是第二个

注意:函数重载的返回类型不规定

引用

//int main()
//{
//  int a = 10;
//  int& b = a;
//
//  cout << &a << endl;
//  cout << &b << endl;
//
//  b++;
//
//  cout << a << endl;
//  cout << b <<endl;
//
//  int& c = a; 
//  int& d = b;
//
//  cout << &a << endl;
//  cout << &b << endl;
//  cout << &c << endl;
//  cout << &d << endl;
//
//  int x = 20;
//  //引用不会改变指向, 所以这里是把x赋值修改c
//  c = x;
//  
//
//  cout << &a << endl;
//  cout << &b << endl;
//  cout << &c << endl;
//  cout << &d << endl;
//
//  //mian函数最后补return 0;
//  return 0;
//}


//void swap(int* p1, int* p2)
//{
//  int tmp = *p1;
//	*p1 = *p2;
//	*p2 = tmp;
//
//}
//
////引用功能1:做函数形参,修改形参影响实参
//void swap(int& p1, int& p2)
//{
//	int tmp = p1;
//	p1 = p2;
//	p2 = tmp;
//}
//
//struct A
//{
//	int a[100];
//	int b;
//	//....
//};
//
//void func(struct A* aa)
//{}
//
////引用功能2:做函数形参, 减少拷贝,提高效率
//void func(struct A& aa)
//{}
//
//
////void swap(int p1, int p2)
////{
////  int tmp = p1;
////	p1 = p2;
////	p2 = tmp;
////
////
////}
//
//
//
//int main()
//{
//  int x = 1, y = 2;
//	swap(&x, &y);
//	swap(x, y);
//
//	struct A a;
//	func(&a);
//	func(a);
//
//	return 0;
//}

引用就是将引用后的变量与目标对象变得任何东西都相同(具体数据相同,指针相同),变量与目标对象就是一个东西

int a = 10;
//  int& b = a;
//
//  cout << &a << endl;
//  cout << &b << endl;

这里打印出来的的结果可以说明引用变量a与变量b的指针相等

int a = 10;
//  int& b = a;
//
//  cout << &a << endl;
//  cout << &b << endl;
//
//  b++;
//
//  cout << a << endl;
//  cout << b <<endl;


这里b++得出的a与b的数据相同

 int& c = a; 
//  int& d = b;
//
//  cout << &a << endl;
//  cout << &b << endl;
//  cout << &c << endl;
//  cout << &d << endl;

接之前引用代码
讲述引用变量用过一次就不能再引用,但是引用变量可以作为本体使其它变量引用它

所以代码abcd指针相等

//  int x = 20;
//  //引用不会改变指向, 所以这里是把x赋值修改c
//  c = x;

接上面引用代码
引用不会改变指向, 所以这里是把x赋值修改c,这实际上我么就把引用变量看成普通变量就行只不过它继承母本的任何性质(就是换了个名字)

////引用功能1:做函数形参,修改形参影响实参
//void swap(int& p1, int& p2)
//{
//	int tmp = p1;
//	p1 = p2;
//	p2 = tmp;
//}

因为p1,p2被引用继承母本任何性质即交换之后就是成功交换

////引用功能2:做函数形参, 减少拷贝,提高效率
//void func(struct A& aa)
//{}
//
//

这个就是要用一个东西承载它不引用的话要重开空间引用就是换个名字即可


文章讲到这就告一段落,希望对你有所帮助,感谢观看!

转载自 CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/2503_94479566/article/details/165124295

文章来源转载

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--