您的位置:首页 > 编程语言 > C语言/C++

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

2015-04-14 19:25 453 查看
贺老师的教学链接 本课讲解

引用作为形参
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: