zoukankan      html  css  js  c++  java
  • 几种参数传递问题(读c++ primer plus)

    1:用数组做参数传递

    #include   <iostream>
    
    #include <cmath>
    
    
    
    struct polar
    {
    
    double distance;:
    
    double angle;
    
    };
    struct rect
    {
    double x;
    double y;
    }
    
    void rect_to_polar(const *pxy,polar *pda);
    void show_polar(const polar *pda);
    
    int main()
    {
    
    rect rplace;
    polar pplace;
    
    while (cin >> rplace.x >> rplace.y)
    {
    rect_to_polar (&rplace,&pplace);
    show_polar(&pplace)
    }
    return 0;
    }
    
    void show_polar(const polar* pda)
    {
        using namespace std;
        cout <<pda->distance;
        cout << pda->angle;
    }
    
    void rect_to_polar (const rect *pxy,polar *pda)
    
    {
    using namespace std;
    pda->distance =
    sqrt(pxy ->x+pda->y*pda->y);
    }
    调用函数时候,将结构的指针的地址而不是结构本身传递给他,这一点很重要。
    3:函数指针:
    1. #include <iostream>
      
    2. 
      
    3. double betsy(int);
      
    4. double pam(int);
      
    5. void estimate (int lines,double (*pf)(int));
      
    6. int main()
      
    7. {
      
    8.     using namespace std;
      
    9.     int code;
      
    10.     cin >> code;
      
    11.     estimate (code,betsy);
      
    12.     estimate(code,pam);
      
    13.     return 0;
      
    14. }
      
    15. double betsy(int lns)
      
    16. {
      
    17.     return 0.05*lns;
      
    18. }
      
    19. double pam (int lns)
      
    20. {
      
    21.     return 0.03*lns + 0.0004*lns*lns;
      
    22. }
      
    23. void estimate (int lines,double(*pf)(int))
      
    24. {
      
    25.     using namespace std;
      
    26.     cout << (*pf)(lines) ;
      
    27. }
      

    第二次调用estimate()函数时候传递了函数pam()的地址。也就是*pf指针。pf=pam

  • 相关阅读:
    AMQP 介绍
    JMS 介绍
    SpringCloud 入门实战(12)--Zipkin(2)--安装使用
    SpringCloud 入门实战(11)--Zipkin(1)--简介
    Kafka 入门实战(3)--SpringBoot 整合 Kafka
    跨域访问方法介绍(9)--nginx 反向代理
    跨域访问方法介绍(8)--使用 WebSocket
    个人网站建好啦!
    Spring Boot配置Slf4j + log4j2
    SpringBoot 解决跨域,3种解决方案
  • 原文地址:https://www.cnblogs.com/sunliming/p/1837739.html
Copyright © 2011-2022 走看看