C++ primer plus 学习笔记(第七章)

第七章 函数—C++的编程模块
7.3.5 指针和const
前提代码:

int grop=16;
int chips=12;
const int *p_snack=&gorp;

const会锁定后面一个符号,所以,此时

p_snack=&chips;    //allowed
*p_snack=20;       //error

前提代码:

int grop=16;
int chips=12;
int* const p_snack=&grop;

此时有:

*p_snack=20;   //right code
p_snack=&gorp;  //wrong code

指针的使用:

#include<iostream>
const int ArSize=8;
int sum_array(const int * begin,const int * end);
int main()
{
    using namespace std;
    int cookies[ArSize] = {1,2,4,8,16,32,64,128};
	int sum=sum_array(cookies,cookies+3);
	return 0;
}
int sum_array(const int* begin,const int * end)
{
	const int * pt;
	int total=0;
	for(pt=begin;pt!=end;pt++)
	{
		total=total+*pt;
	}
	return total;
}

7.4 函数和二维数组
为编写将数组作为参数的函数,必须牢记,数组名被视为其地址。

7.6.3 传递结构的地址
使用指向结构的的指针来传递结构,相比于直接传递结构,有以下三个地方需要修改:
调用函数时,将结构的地址(&pplace)而不是结构本身(pplace)传递给它;
将形参声明为指向polar的指针,即polar*类型,因此使用了const修饰符
由于形参是指针而不是结构,因此应使用间接成员(->),而不是成员运算符(.)

// strctptr.cpp -- functions with pointer to structure arguments
#include <iostream>
#include <cmath>
// structure templates
struct polar
{
    double distance;      // distance from origin
    double angle;         // direction from origin
};
struct rect
{
    double x;             // horizontal distance from origin
    double y;             // vertical distance from origin
};
// prototypes
void rect_to_polar(const rect * pxy, polar * pda);
void show_polar (const polar * pda);
int main()
{
    using namespace std;
    rect rplace;
    polar pplace;
    cout << "Enter the x and y values: ";
    while (cin >> rplace.x >> rplace.y)
    {
        rect_to_polar(&rplace, &pplace);    // pass addresses
        show_polar(&pplace);        // pass address
        cout << "Next two numbers (q to quit): ";
    }
    cout << "Done.\n";
    return 0;
}
// show polar coordinates, converting angle to degrees
void show_polar (const polar * pda)
{
    using namespace std;
    const double Rad_to_deg = 57.29577951;
    cout << "distance = " << pda->distance;
    cout << ", angle = " << pda->angle * Rad_to_deg;
    cout << " degrees\n";
}
// convert rectangular to polar coordinates
void rect_to_polar(const rect * pxy, polar * pda)
{
    using namespace std;
    pda->distance =
        sqrt(pxy->x * pxy->x + pxy->y * pxy->y);
    pda->angle = atan2(pxy->y, pxy->x); 
}

7.10 函数指针
与数据项类似,函数也有地址。
假设要涉及一个名为estimate( )的函数,估算编写指定行数的代码所需的时间。为实现该目标,采用的机制是,将程序员要使用的算法函数的地址传递给estimate()。为此,必须能够完成下面的工作:
获取函数的地址
声明一个函数指针
使用函数指针来调用函数
1)获取函数的地址
获取函数的地址很简单:只要使用函数名(后面不跟参数)即可
2)声明函数指针

double pam(int);
double (*pf)(int);
pf=pam                 //pf now points to the pam() function

(*pf)也是函数,所以pf就是函数指针,是一个指向函数的指针
3) 使用指针来调用函数

double pam(int);
double (*pf)(int);
pf=pam;
double x=pam(4);
double y=(*pf)(5);
/*..............*/
double y=pf(5);   //also call pam() using the pointer pf

代码演示:

// fun_ptr.cpp -- pointers to functions
#include <iostream>
double betsy(int);
double pam(int);
// second argument is pointer to a type double function that
// takes a type int argument
void estimate(int lines, double (*pf)(int));
int main()
{
    using namespace std;
    int code;
    cout << "How many lines of code do you need? ";
    cin >> code;
    cout << "Here's Betsy's estimate:\n";
    estimate(code, betsy);
    cout << "Here's Pam's estimate:\n";
    estimate(code, pam);
    // cin.get();
    // cin.get();
    return 0;
}
double betsy(int lns)
{
    return 0.05 * lns;
}
double pam(int lns)
{
    return 0.03 * lns + 0.0004 * lns * lns;
}
void estimate(int lines, double (*pf)(int))
{
    using namespace std;
    cout << lines << " lines will take ";
    cout << (*pf)(lines) << " hour(s)\n";
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值