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; }
相关文章推荐
- C++语言基础 例程 有默认参数的函数
- C++语言基础 例程 函数重载
- C++语言基础 例程 函数模板
- C++语言基础 例程 类声明和成员函数定义的分离
- C++语言基础 例程 类的成员函数
- C++语言基础 例程 对象成员的引用
- C++语言基础 例程 C++中的输入和输出
- C++语言基础 例程 纯虚函数
- c++ 基础知识(输入输出、namespace命名空间、引用、函数重载模版、内存管理)
- C++语言基础 例程 案例:bmp文件格式剖析
- C++语言基础 例程 基类与派生类的转换
- C++语言基础 例程 命名空间要解决的问题
- C++基础---有返回值类型函数(返回非引用类型)
- C++语言基础 例程 内置函数
- C++语言基础 例程 抽象类
- C++语言基础 例程 运算符重载的方法
- C++语言基础 例程 文件的随机读写
- C++语言基础 例程 命名空间的使用
- C++语言基础 例程 设计模式简介
- C++语言基础 例程 对象的动态建立和释放