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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值