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";}
    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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回调函数在C语言是一个函数指针,用于将一个函数作为参数传递给另一个函数,并在特定事件发生时被调用。 下面是一个简单的C语言例程,演示了如何使用回调函数: ```c #include <stdio.h> // 定义回调函数的原型 typedef void (*Callback)(int); // 接收一个回调函数作为参数的函数 void performTask(int num, Callback callback) { printf("Performing task with number: %d\n", num); printf("Task completed!\n"); // 调用回调函数 callback(num); } // 回调函数的实现 void callbackFunction(int num) { printf("Callback function called with number: %d\n", num); } int main() { int num = 10; // 调用 performTask 函数,并传递回调函数作为参数 performTask(num, callbackFunction); return 0; } ``` 在上述例程,我们定义了一个回调函数 `Callback`,它接受一个整数参数并返回 `void`。然后,我们定义了一个 `performTask` 函数,它接受一个整数参数和一个回调函数作为参数。在 `performTask` 函数内部,我们执行了一些任务,并在任务完成后调用了传递的回调函数。 在 `main` 函数,我们定义了一个整数变量 `num`,然后调用了 `performTask` 函数,并将 `callbackFunction` 作为回调函数传递进去。当任务完成时,回调函数 `callbackFunction` 被调用,并打印出传递的整数参数。 这是一个简单的例子来演示回调函数在C语言的使用。实际上,回调函数可以用于更复杂的场景,例如事件处理、异步编程等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值