第七天2017/04/14(引用与const、C++基本知识)

来源:互联网 发布:手机在linux查arp 编辑:程序博客网 时间:2024/06/16 23:57

一、引用与const

1const引用的初始化的两种形式://const引用,让被const修饰的变量具有只读属性#include <iostream>using namespace std;int main(){   //给常引用初始化,有两种方法:    //1.让变量初始化const引用    int  a = 10;    const int &b = a;    //b = 100; //不能通过b去修改a,编译错误    a = 100;   //正确,但是可以修改a本身,此时a和b都变成修改后的100    printf("%d,%d",a,b);    //2.使用字面常量初始化const引用    //int &c = 10; //失败,因为10是字面量,字面量10没有分配内存空间    const int &d = 10;  //成功,加上const,C++编译器会给10分配内存空间}============================================================2、接着就是难点:分析下面三个输出结果,哪个输出是乱码?#include <iostream>using namespace std;//问:打印出来的b1、b2、b3哪个是乱码?为什么?int f1(){    int a;    a = 11;    return a;}int& f2()  //返回一个局部变量a的引用{    int a;    a = 11;    return a;}int main(){//用引用的本质去剖析它:引用的本质是const type *name,C++编译器会对引用执行*name操作    int  b1 = f1();  //OK//重点分析下面两句话:    int  b2 = f2();  //返回一个值,赋值给b2    int& b3 = f2();      printf("b1 = %d\n",b1);  //  11    printf("b2 = %d\n",b2);  //  11    printf("b3 = %d\n",b3);  //  乱码}【解析】int  b2 = f2();//在f2()中给a分配内存,在main中给b2也分配内存,在f2()没有执行完时,        //用b2把f2()返回的值接过来,相当于分配了两个内存(a一个,b2一个),因此        //b2打印出来的不是乱码int& b3 = f2();  //b3是一个引用类型,C++编译器会帮我们执行*b3操作,又            //由于a在f2()调用后内存地址被回收,此时*b3打印出来是一个乱码。【先背下来】返回指针的引用int& f(); 当用int b2 = f();接受时,正确;当用int& b3 = f();接受时,错误。3、“结构体指针的引用作形参、结构体二级指针” 的对比  #include <iostream>using namespace std;struct student{    int age;    char name[10];};void getStudent1(struct student* *stu) {    struct student* pTmp = NULL;    pTmp = (struct student*)malloc(sizeof(struct student)); //创建一个结构体指针,并分配内存    pTmp->age = 24;    strcpy(pTmp->name,"Mr.g");    *stu = pTmp;}void getStudent2(struct student* &pTmp) //重点讲解结构体指针的引用{    pTmp = (struct student*)malloc(sizeof(struct student));    pTmp->age = 22;    strcpy(pTmp->name,"Mr.w");}int main(){       struct student* p1 = NULL;    struct student* p2 = NULL;//用二级指针(作形参)处理一级指针(作实参)    getStudent1(&p1);//用一级指针的引用(作形参)处理一级指针(作实参)    getStudent2(p2);    cout<<p1->age<<"  "<<p1->name<<endl;    cout<<p2->age<<"  "<<p2->name<<endl;}=================================================================4const结构体引用注://const int& a;  等价于const int* const a;void getStudent3(const struct student &pTmp)//const引用修饰的变量pTmp{//1.pTmp本身不能被修改    //struct student stu;      //pTmp = stu;           //2.pTmp变量的元素不能被改变    //pTmp->age = 22;          //strcpy(pTmp->name,"Mr.w");}=================================================================5//const变量 可以替代#define宏常数//inline函数可以替代#define表达式#include <iostream>using namespace std;#define a1 10#define func1(a,b)  ((a)<(b)?(a):(b))//const变量 可以替代#define宏常数//inline函数可以替代#define表达式const int a2 = 10;inline int func2(int a,int b) //注:内联函数的函数体必须和函数实现写在一起{//内联函数没有普通函数调用时的内存开销(压栈、跳转、返回)    return a<b?a:b;}int main(){    int a =1,b = 3;    printf("%d\n",func1(++a,b));    //宏定义:结果不是2,竟然是3,会发生令人“蛋疼的问题”,产生副作用!    //printf("%d\n",func2(++a,b));  //内联函数:2}【解释】func1(++a,b) ==> ((++a)<(b)?(++a):(b))

二、C++基本知识

1、函数默认参数的规则    声明:void f(int a,int b,int c = 100);    调用:f(1,2);         f(1,2,3);2、函数占位参数:占位参数只有参数类型声明,没有参数名声明声明int f(int a,int b,int ,float)    {        return a+b;    }调用:    //int result1 = f(1,2); //编译失败     int result2 = f(1,2,3,4); 3、函数默认参数和占位参数混搭声明int f(int a,int b,int = 0 ,float = 0.0)    {        return a+b;    }调用:    //int result1 = f(1,2); //编译成功    int result2 = f(1,2,3,4); 4、函数重载:并不是C++的特性,函数的重写才是5、函数重载和函数指针
1 0
原创粉丝点击