C++语言基础 例程 函数中的引用

简介: 贺老师的教学链接  本课讲解引用作为形参#include<iostream>using namespace std;class Sample{ int x;public: Sample(int a): x(a) {cout<<"A";} Sample(Sample &a): x(a.x) {cout<<"B"

贺老师的教学链接  本课讲解


引用作为形参

#include<iostream>
using namespace std;
class Sample
{
    int x;
public:
    Sample(int a): x(a) {cout<<"A";}
    Sample(Sample &a): x(a.x) {cout<<"B";}
    int getX(){return x;}
};
void disp(Sample &s){cout<<s.getX();}
int main()
{
    Sample s1(2),s2(s1);
    disp(s1);
    return 0;
}


如果参形不用引用

class Sample
{
    int x;
public:
    Sample(int a): x(a) {cout<<"A";}
    Sample(Sample &a): x(a.x) {cout<<"B";}
    int getX(){return x;}
};
void disp(Sample s){cout<<s.getX();}
int main()
{
    Sample s1(2),s2(s1);
    disp(s1);
    return 0;
}


 

函数返回值——简单的返回值说起

#include <iostream>
using namespace std;
int aaa()
{
    int a = 5;
    return a; //值
}
int *bbb()
{
    int b[5]= {0};
    return b;//返回栈区的指针,潜在风险!有警告
}
 
int *ccc()
{
    static int c = 100;
    return &c; //静态区指针
}
 
int *ddd()
{
    int *p = new int(20);
    return p; //堆区指针
}
 
int main()
{
    int n = aaa();
    int *p1 = bbb();
    int *p2 = ccc();
    int *p3 = ddd();
    int b = 38;
    cout<<n<<endl;
    cout<<*p1<<endl;
    cout<<*p2<<endl;
    cout<<*p3<<endl;
    delete p3;
    return 0;
}


返回值为引用

#include <iostream>
using namespace std;
int aaa()
{
    int a = 5;
    return a; //值
}
 
int &bbb()
{
    int b = 0;
    return b;//返回对局部变量的引用,潜在风险!有警告
}
 
int &ccc()
{
    static int c = 100;
    return c; //值
}
 
int main()
{
    int n1 = aaa();
    int &n2 = bbb();
    int &n3 = ccc();
    cout<<n1<<endl;
    cout<<n2<<endl;
    cout<<n3<<endl;
    return 0;
}


 

返回值为非引用对象,返回值直接取栈中的结果

#include<iostream>
using namespace std;
class Sample
{
    int x;
public:
    Sample(){}
    Sample(int a): x(a) {cout<<"A";}
    ~Sample(){cout<<"B";}
    int getX(){return x;}
};
 
Sample copySample(Sample &a)
{
    Sample b(a.getX());
    cout<<"C";
    return b;
}
 
void disp(Sample &s)
{
    cout<<s.getX();
}
 
int main()
{
    Sample s1(2),s2;
    s2=copySample(s1);
    disp(s2);
    return 0;
}


返回值为引用对象时

#include<iostream>
using namespace std;
class Sample
{
    int x;
public:
    Sample(){}
    Sample(int a): x(a) {cout<<"A";}
    Sample(Sample &a): x(a.getX()){cout<<"D";}
    ~Sample(){cout<<"B";}
    int getX(){return x;}
};
 
Sample& copySample(Sample &a)
{
    Sample b(a.getX());
    cout<<"C";
    return b;
}
 
void disp(Sample &s)
{
    cout<<s.getX();
}
 
int main()   //(1)   输出AACBD2BB的测试函数
{
    Sample s1(2),s2=copySample(s1);
    disp(s2);
    return 0;
}
int main()   //(2)   输出AACB2BB的测试函数
{
    Sample s1(2),s2;
    s2=copySample(s1);
    disp(s2);
    return 0;
}


可以这样做!

#include<iostream>
using namespace std;
class Sample
{
    int x;
public:
    Sample(){}
    Sample(int a): x(a) {cout<<"A";}
    Sample(Sample &a): x(a.getX()){cout<<"D";}
    ~Sample(){cout<<"B";}
    int getX(){return x;} const
    void setX(int i){x=i;}
};
 
Sample& copySample(Sample &a, Sample &b)
{
    b.setX(a.getX());
    cout<<"C";
    return b;
}
 
void disp(Sample &s)
{
    cout<<s.getX();
}
 
int main()
{
    Sample s1(2),s2;
    s2=copySample(s1,s2);
    disp(s2);
    return 0;
}



目录
相关文章
|
2月前
|
存储 C++
C++语言中指针变量int和取值操作ptr详细说明。
总结起来,在 C++ 中正确理解和运用 int 类型地址及其相关取值、设定等操纵至关重要且基础性强:定义 int 类型 pointer 需加星号;初始化 pointer 需配合 & 取址;读写 pointer 执向之处需配合 * 解引用操纵进行。
169 12
|
7月前
|
存储 负载均衡 算法
基于 C++ 语言的迪杰斯特拉算法在局域网计算机管理中的应用剖析
在局域网计算机管理中,迪杰斯特拉算法用于优化网络路径、分配资源和定位故障节点,确保高效稳定的网络环境。该算法通过计算最短路径,提升数据传输速率与稳定性,实现负载均衡并快速排除故障。C++代码示例展示了其在网络模拟中的应用,为企业信息化建设提供有力支持。
182 15
|
4月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
95 0
|
7月前
|
存储 算法 安全
企业员工数据泄露防范策略:基于 C++ 语言的布隆过滤器算法剖析[如何防止员工泄密]
企业运营过程中,防范员工泄密是信息安全领域的核心议题。员工泄密可能致使企业核心数据、商业机密等关键资产的流失,进而给企业造成严重损失。为应对这一挑战,借助恰当的数据结构与算法成为强化信息防护的有效路径。本文专注于 C++ 语言中的布隆过滤器算法,深入探究其在防范员工泄密场景中的应用。
119 8
|
7月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
362 6
|
12月前
|
程序员 C++ 容器
在 C++中,realloc 函数返回 NULL 时,需要手动释放原来的内存吗?
在 C++ 中,当 realloc 函数返回 NULL 时,表示内存重新分配失败,但原内存块仍然有效,因此需要手动释放原来的内存,以避免内存泄漏。
|
12月前
|
算法 C++
2022年第十三届蓝桥杯大赛C/C++语言B组省赛题解
2022年第十三届蓝桥杯大赛C/C++语言B组省赛题解
249 5
|
12月前
|
存储 前端开发 C++
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
448 6
|
12月前
|
C++
C++ 多线程之线程管理函数
这篇文章介绍了C++中多线程编程的几个关键函数,包括获取线程ID的`get_id()`,延时函数`sleep_for()`,线程让步函数`yield()`,以及阻塞线程直到指定时间的`sleep_until()`。
199 0
C++ 多线程之线程管理函数
|
12月前
|
编译器 C语言 C++
C++入门6——模板(泛型编程、函数模板、类模板)
C++入门6——模板(泛型编程、函数模板、类模板)
184 0
C++入门6——模板(泛型编程、函数模板、类模板)