C++day3 指针&引用

本文围绕C++展开,介绍了指针的引用,包括函数返回引用时栈变量和静态/全局变量的不同使用情况,还涉及链式编程和运算符重载。同时阐述了C++对C的拓展,如内联函数、默认参数、函数占位参数、函数重载,以及函数指针的基础语法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

09指针的引用
c++引用的难点
    当函数返回值为引用时
    若返回栈变量
    不能成为其它引用的初始值
    不能作为左值使用
若返回静态变量或者全局变量
    可以成为其他引用的初始值
    即可以作为左值使用也可以作为右值使用
c++链式编程中,经常使用到引用,运算符重载专题。

//间接赋值
#include "iostream"
using namespace std;

struct Teacher
{
    char name[64];
    int age;
};

//在被调用函数,获取资源
int getTeacher(Teacher **p)
{
    Teacher *tmp = NULL;
    tmp = (Teacher *)malloc(sizeof(Teacher));
    if(p == NULL)
    {
        return -1;    
    }
    if(tmp == NULL)
    {
        return -2;    
    }
    //p是实参的地址,*实参的地址 来间接的修改实参的值
    tmp->age = 33;
    *p = tmp;
}

void main(){
    Teacher *pT1 = NULL;
    getTeacher(&pT1);
    
    cout<<"age:"<<pT1->age<<endl;
    cout << "hello"<<endl;

    system("pause");
}


//换成c++的引用方式
#include "iostream"
using namespace std;

struct Teacher
{
    char name[64];
    int age;
};

//在被调用函数,获取资源
int getTeacher(Teacher **p)
{
    Teacher *tmp = NULL;
    tmp = (Teacher *)malloc(sizeof(Teacher));
    if(p == NULL)
    {
        return -1;    
    }
    if(tmp == NULL)
    {
        return -2;    
    }
    //p是实参的地址,*实参的地址 来间接的修改实参的值
    tmp->age = 33;
    *p = tmp;
}

int getTeacher2(Teacher * &myp)
{
    //给myp赋值相当于给main中的pT1赋值
    myp = (Teacher *)malloc(sizeof(Teacher));
    if(myp == NULL){
        return -1;
    }
    myp->age = 36;

}
void FreeTeacher( Teacher *pT1){
    if(pT1 == NULL)
    {
        return ;
    }
    free(pT1);
}

void main(){
    Teacher *pT1 = NULL;

    //1 c语言中的2级指针
    getTeacher(&pT1);
    cout<<"age:"<<pT1->age<<endl;
//    FreeTeacher(pT1);
    //2 c++中的引用(指针的引用)
    
    getTeacher2(&pT1);
    cout<<"age:"<<pT1->age<<endl;
    FreeTeacher(pT1);

    cout << "hello"<<endl;

    system("pause");
}
//二级指针
#include <iostream>
using namespace std;

//
struct Teacher{
    char name[64];
    int age;
    
};
//在被调用函数 获取资源
int getTeacher(Teacher **p){
        Teacher *tmp  = NULL;
        
        if(p == NULL){
            return  -1;
        }
        tmp = (Teacher *)malloc(sizeof(Teacher));
        if(tmp ==NULL){
            return -2;
        }
        tmp->age = 33 ;
        *p  = tmp;
    
}
void main(){
    Teacher *p1 = NULL;
    getTeacher(&p1);

    cout <<p1->age<<endl;

    cout<<"hello"<<endl;

    system("pause");
}

//常引用

#include<iostream>
using namespace std;


void main061903(){
//普通引用
    int a =10;
    int &b =a ;

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

    //常引用
    int x  = 20;
    const int &y = x;//常量引用,拥有只读属性,不能通过y修改x

    //y = 21;
    //1.用变量初始化常引用
    {
    int x1 = 30;
    const int &y1 = x1;//用x1变量初始化常引用
    }
    //2.用字面量 初始化 常量引用
    {
        const int a = 10; //c++编译器把a放在符号表中
        //int &m = 41;//普通引用引用一个字面量,字面量没有内存地址
        const int &m = 43;//现在,c++编译器就会给常引用&m分配内存空间了
        cout<<&m<<endl;
        cout<<m<<endl;
    }


    cout<<"hello"<<endl;
    
    system("pause");
    return;
}

//常引用简单理解代码
#include <iostream>
using namespace std;


struct Teacher{
    char name[64];
    int age;
    
};
//在被调用函数 获取资源

void printTeacher(const Teacher &myt){

    //常引用让 实参变量拥有只读属性
    //myt.age = 66;//这里不能修改,会报错
    printf("my.age=%d\n",myt.age);
}
void main(){
    Teacher t1;
    t1.age = 52;
    printTeacher(t1);

    cout<<"hello"<<endl;

    system("pause");
}


//普通引用相当于  int * const e;
//常量引用相当于const int * const e;
//使用字面量对const引用初始化的时候,会分配一个内存空间
//使用字面量初始化引用,字面量拥有只读属性


07C++对c的拓展
//内联函数,C++中使用内联函数替代宏代码片段
//C++中使用inline关键字声明,直接将函数体插入在函数调用的地方
//内联函数没有函数调用时候额外的开销,压栈跳转返回什么的,没有
//内联函数是一种特殊的函数,具有普通函数的特征
//但是内联函数是编译器处理,是对编译器的一种请求,编译器有可能拒绝这种请求。
//宏代码片段由预处理器处理,进行简单的文本交换,没有任何编译过程
//现代C++编译器能进行编译优化,因此一些函数没有inline声明,也可以被编译器内联编译,一些现代的编译器提供了拓展语法, 可以对函数进行强制内联。
//内联函数中不允许任何形式的循环语句,不能存在过多的条件语句,函数体不可以过大。
#include<iostream>
using namespace std;

inline void printA(){
    cout<<"yj is a smelly boy"<<endl;
}

void main061905(){

    printA();
    cout<<"hello"<<endl;
    system("pause");
}


//默认参数。C++可以在函数声明时候提供一个默认值
#include<iostream>
using namespace std;

void myprint1(int x=3){

    cout<<"X"<<x<<endl;
}
void myprint2(int m,int x =3,int y=4){

    cout<<"X"<<x<<endl;
}
//函数占位参数,函数调用时候必须写够参数

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


//默认参数和占位参数在一起
void func2(int a,int b,int =0)
{
    cout<<"a"<<a<<"b"<<b<<endl;
}

 

void main061907(){
    myprint1(4);
    myprint1();
    //func1(1,2);
    func1(1,2,3);

    func2(1,2);//ok
    func2(1,2,3);//ok
    cout<<"hello"<<endl;
    system("pause");
}

 

//函数重载,名称,参数,返回值不是函数重载的判断标准
//当函数重载和默认参数同时出现会出现二义性,编译器不允许这种情况出现
//函数指针的基础语法
typedef void (myTypeFunc)(int a,int b)
//定义一个函数指针,这个指针指向函数的入口地址
typedef void (*myPTypeFunc)(int a,int b);
//声明了一个指针的数据类型
void (*myVarPFunc)(int a,int b)
//定义一个函数指针 变量

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值