C++函数引用

 1 #include<iostream>
  2 using namespace std;
  3 int f();//C++必须先声明,C中可有可无
  4 void f1(){cout<<"hello"<<endl;}
  5 void f2(void){return f1();}
  6 void f3(double){cout<<"world"<<endl;}//有类型 无名字 : 哑元函数(一般是基于>    兼容考虑)
  7 //函数的重载 overload
  8 void JiaoHuan(int* a,int* b){int t = *a;*a = *b;*b = t;}//C做法
  9 void JiaoHuan(int& a,int& b){int t = a;a = b;b = t;}//C++做法
 10 void print(const int &n)//形参尽量加const 不修改原数据,加了const会修改原数>    据
 11 {cout<<&n<<'\t'<<hex<<showbase<<n<<endl;}//hex是以16进制输出,showbase是指加
    前缀输出
 12 
 13 struct Window
 14 {
 15         string text;
 16         int x,y;
 17         int width,height;
 18 };//GUI
 19 void input(Window &r)//(Window *p)

 20 {

21         cout<<"请输入 窗口标题,xy坐标,宽度高度"<<endl;
 22 //      cin>> p->text >> p->x >> p-> y >> p-> width << p->height << endl;
 23         cin>> r.text >> r.x >> r.y >> r.width >> r.height;
 24 }
 25 
 26 void print(const Window &r)//const只读方式访问
 27 {
 28         cout<<"=========="<<r.text<<"=========="<<endl;
 29         cout<<"=========="<<r.x<<"=========="<<r.y<<endl;
 30         cout<<"=========="<<r.width<<"=========="<<r.height<<endl;
 31 
 32 }
 33 
 34 int main()
 35 {
 36         Window w;
 37         input(w);
 38         print(w);
 39 
 40         //f1(123);函数f1是无参的
 41         f1();
 42         f2();
 43         f3(123.4);

 44 
 45         int m = 10,n = 20;
 46         JiaoHuan(&m,&n);
 47         cout<<m<<" , "<<n<<endl;
 48         JiaoHuan(m,n);
 49         cout<<m<<" , "<<n<<endl;
 50 
 51         void(*p)(int&,int&) = &JiaoHuan;
 52         p(m,n);
 53         cout<<m<<" , "<<n<<endl;
 54         cout<<"&m = "<<&m<<",&n = "<<&n<<endl;
 55         print(m);
 56         print(n);
 57         print(123);
 58         print(m+n);
 59 
 60         return 0;
 61 }
 62 
 63 int f()
 64 {
 65         cout<<"c functon"<<endl;
 66         return 0;

 67 }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值