第27章 C语言
1. C与C++
2. C与C++的兼容性
3. C不支持的C++特性
4. C中函数与C++的区别:不支持函数重载
5. 函数参数检查
6. 函数定义
7. 在C++中调用C和在C中调用C++
8. 函数指针
/*
//
// This is example code from Chapter 27.2.4 "Pointers to functions" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
*/
/*----------------------------------------------------------------------------*/
struct Shape1 {
enum Kind { circle, rectangle } kind;
/* ... */
};
/*----------------------------------------------------------------------------*/
void draw(struct Shape1* p)
{
switch (p->kind) {
case circle:
/* draw as circle */
break;
case rectangle:
/* draw as rectangle */
break;
}
}
/*----------------------------------------------------------------------------*/
int f(struct Shape1* pp)
{
draw(pp);
/* ... */
}
/*----------------------------------------------------------------------------*/
typedef void (*Pfct0)(struct Shape2*);
typedef void (*Pfct1int)(struct Shape2*,int);
/*----------------------------------------------------------------------------*/
struct Shape2 {
Pfct0 draw;
Pfct1int rotate;
/* ... */
};
/*----------------------------------------------------------------------------*/
void draw1(struct Shape2* p)
{
(p->draw)(p);
}
/*----------------------------------------------------------------------------*/
void rotate(struct Shape2* p)
{
(p->rotate)(p,90);
}
/*----------------------------------------------------------------------------*/
int f1(struct Shape * pp)
{
draw(pp);
/* ... */
}
/*----------------------------------------------------------------------------*/
//
// This is example code from Chapter 27.2.4 "Pointers to functions" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
*/
/*----------------------------------------------------------------------------*/
struct Shape1 {
enum Kind { circle, rectangle } kind;
/* ... */
};
/*----------------------------------------------------------------------------*/
void draw(struct Shape1* p)
{
switch (p->kind) {
case circle:
/* draw as circle */
break;
case rectangle:
/* draw as rectangle */
break;
}
}
/*----------------------------------------------------------------------------*/
int f(struct Shape1* pp)
{
draw(pp);
/* ... */
}
/*----------------------------------------------------------------------------*/
typedef void (*Pfct0)(struct Shape2*);
typedef void (*Pfct1int)(struct Shape2*,int);
/*----------------------------------------------------------------------------*/
struct Shape2 {
Pfct0 draw;
Pfct1int rotate;
/* ... */
};
/*----------------------------------------------------------------------------*/
void draw1(struct Shape2* p)
{
(p->draw)(p);
}
/*----------------------------------------------------------------------------*/
void rotate(struct Shape2* p)
{
(p->rotate)(p,90);
}
/*----------------------------------------------------------------------------*/
int f1(struct Shape * pp)
{
draw(pp);
/* ... */
}
/*----------------------------------------------------------------------------*/