C++学习(5)(函数 指针 引用)

函数

返回值 基本的区别就是可以返回auto

参数的区别 有 可以有栈区参数 变参数

对于参数还有一个老生长谈的问题 什么是实参什么是形参 我觉得形参就是你自己在写函数参数的时候自己命名的 而实参是你自己传入的,我们也可以把他俩的地址找出来看看他们在哪一个部分的内存

实参会复制给形参而不是地址直接引用

而且函数结束后形参的内存就会被释放掉

但是在这此学习对return 也有了新的理解

可以把他理解位能赋值的break

cpp 复制代码
#include <iostream>

#include <string>

#include <vector>

using namespace std;



//函数的定义

//返回值类型 函数1名(形参类型 变量名)



bool View(int index)

{

    cout << "call view " << index << "" << endl;



    return true;

    //return 可以把他理解位能赋值的break

}

//函数的参数 形参和实参

//参数可以有0个或者多个用,隔开

//参数无void可以省略



void Setsize(int w, int h)

{

    cout << &w << ":" << &h<<endl;

}



//C++11自动推导返回值

auto autofu(int w, int h)

{

    w += h;



    return w;

}



int main()

{

    int w{ 100 };

    int h{ 120 };

    cout << &w << ":" << &h<<endl;

    Setsize(w, h);

    //实参会复制给形参而不是地址直接引用



    if (View(100))

    {

        cout << "ok" << endl;

    }



    cout << autofu(1, 2) << endl;



    std::cout << "Hello World!\n";

}

变量的做用域和函数的关系

全局变量是一个把双刃剑

好处是可以全局访问坏处是它可以全局范围访问

这样他的变化我们不能具体确定他是在哪里变化的

那我们有没有一种变量他又有全局的生命周期但是只能局部访问呢

有的兄弟,有的

和C语言一样C++也有静态变量

静态变量也分全局和局部

静态全局是只有本文件可以访问

静态局部变量只有在这个{}可以访问

起始static就是扩大了生命周期

指针和引用

指针就是内存地址

指针定量是存放指针的变量

引用是C++的新东西

起始引用就是把变量给替换了像是给他起了一个别名

cpp 复制代码
#include <iostream>

using namespace std;



//指针和引用



int main()

{

    //指针

    int x{ 100 };

    cout << "x  " << x << endl;

    cout << "&x  " << &x << endl;

    int* px = &x;

    cout << "*px  " << *px << endl;//*px存着值

    cout << "px  " << px << endl;//px存着x的地址

    cout << "&px  " << &px << endl;//&px是px的地址

    (*px)++;

    cout << "x  " << x << endl;



    //引用 别名

    int& ref{ x };//必须初始化

    cout << "ref  " << ref << endl;

    cout << "&ref  " << &ref << endl;//引用本身不占内存

    std::cout << "Hello World!\n";

}

指针和引用与函数结合

提高效率 修改用户数据(多个返回值) 直接控制内存(嵌入式 快)

在获得多个返回值的这个作用中有一个不成文的规定就是不改变变量的指针要加const

在于函数传递的时候引用的调用方式和普通参数一样

cpp 复制代码
#include <iostream>

using namespace std;

//指针和引用作为参数减少复制

void Test1(const int* x1, const int* y1)//变量不改变要加const

{

    cout << "*x  " << *x1 << "  *y  " << *y1 << endl;

    cout << "x  " << x1 << "y  " << y1 << endl;

}



void Test2(const int& x2, const int& y2)//变量不改变要加const

{

    cout << "x  " << x2 << "  y  " << y2 << endl;

    cout << "&x  " << &x2 << "  &y2  " << &y2 << endl;

}



void Test3( int* x1,  int* y1)//变量不改变要加const

{

    (*x1)++; (*y1)++;

}



void Test4( int& x2,  int& y2)//变量不改变要加const

{

    x2++; y2++;

}



//都可以直接改变值



int main()

{

    int x{ 100 };

    int y{ 100 };

    Test1(&x, &y);

    Test2(x, y);

    Test3(&x, &y);

    cout << "x  " << x << "  y  " << y << endl;

    Test4(x, y);

    cout << "x  " << x << "  y  " << y << endl;



    std::cout << "Hello World!\n";

}

要注意的是指针和引用都不能返回栈区变量

只能返回全局变量,堆区变量,静态变量

Auto变量类型不能直接推导出引用,但是指针可以

cpp 复制代码
#include <iostream>

using namespace std;

//指针和引用作为参数减少复制

void Test1(const int* x1, const int* y1)//变量不改变要加const

{

    cout << "*x  " << *x1 << "  *y  " << *y1 << endl;

    cout << "x  " << x1 << "y  " << y1 << endl;

}



void Test2(const int& x2, const int& y2)//变量不改变要加const

{

    cout << "x  " << x2 << "  y  " << y2 << endl;

    cout << "&x  " << &x2 << "  &y2  " << &y2 << endl;

}



void Test3( int* x1,  int* y1)//变量不改变要加const

{

    (*x1)++; (*y1)++;

}



void Test4( int& x2,  int& y2)//变量不改变要加const

{

    x2++; y2++;

}



int* Test5(void)

{

    int x = 0;

    static int y = 0;

    //return &x;//错误无法传递栈区变量的地址

    return (&y);

}



int& Test6(void)

{

    int x = 0;

    static int y = 0;

    //return &x;//错误无法传递栈区变量的地址

    return y;

}



//都可以直接改变值



int main()

{

    int x{ 100 };

    int y{ 100 };

    Test1(&x, &y);

    Test2(x, y);

    Test3(&x, &y);

    cout << "x  " << x << "  y  " << y << endl;

    Test4(x, y);

    cout << "x  " << x << "  y  " << y << endl;



    std::cout << "Hello World!\n";

}
相关推荐
forestsea2 小时前
Spring Cloud Alibaba 2025.1.0.0 正式发布:拥抱 Spring Boot 4.0 与 Java 21+ 的新时代
java·spring boot·后端
IT枫斗者2 小时前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!
java·开发语言·前端·javascript·spring boot·后端·intellij-idea
forestsea2 小时前
Spring Boot 4.0 + JDK 25 + GraalVM:下一代云原生Java应用架构
java·spring boot·云原生
♡喜欢做梦2 小时前
Spring Boot 日志实战:级别、持久化与 SLF4J 配置全指南
java·spring boot·后端·spring·java-ee·log4j
紫陌涵光2 小时前
669. 修剪二叉搜索树
算法·leetcode
青衫码上行2 小时前
【项目部署】Spring Boot项目部署的四种方式
java·linux·服务器·spring boot·后端·docker·腾讯云
想不明白的过度思考者2 小时前
你真的会打印日志吗?基于 Spring Boot 的全方位日志指南
java·spring boot·后端·日志