程序结构

程序由以下文件组成:
  • 包含以下内容的 C++ 源文件 hourclas.cpp :
    • 一个基类 HourMin和两个派生类 HourMinSec1HourMinSec2 的定义
    • 具有 extern "C" 链接的三个函数原型:
      • extern "C" void CSetHour(HourMin *)
      • extern "C" void CSetSec(HourMin *)
      • extern "C" void CSafeSetHour(HourMin *)
    • 具有 extern "C" 链接的函数的定义, extern "C" void CXXSetHour(HourMin * x)
    • 包含程序逻辑的 main() 函数
    请参阅 C++ 源文件 hourclas.cpp 定义由 C 源文件 hour.c使用。
  • 包含以下内容的 C 源文件 hour.c :
    • 映射到文件 hourclas.cpp 中的 C++ 类 HourMin 的结构 CHourMin
    • hourclas.cpp 中声明的具有 extern "C" 链接的三个函数的定义
    • 函数 CSafeSetHour() 的定义

C++ 源文件 hourclas.cpp C 源文件使用的定义 hour.c

#include <iostream.h>

class HourMin {        // base class
  protected:
    int h;
    int m;

  public:
    void set_hour(int hour) { h = hour % 24; }  // keep it in range
    int  get_hour()         { return h; }
    void set_min(int min)   { m =  min % 60; } // keep it in range
    int  get_min()          { return m; }
    HourMin(): h(0), m(0) {}
    void display() { cout << h << ':' << m << endl; }
};

// derived from class HourMin
class HourMinSec1 : public HourMin {
  public:
    int s;
    void set_sec(int sec) { s = sec % 60; }  // keep it in range
    int  get_sec()        { return s; }
    HourMinSec1() { s = 0; }
    void display() { cout << h << ':' << m << ':' << s << endl; }
};

// has an HourMin contained inside
class HourMinSec2 {
  private:
    HourMin a;
    int s;

  public:
    void set_sec(int sec) { s = sec % 60; }  // keep it in range
    int  get_sec()        { return s; }
    HourMinSec2()         { s = 0; }
    void display() {
       cout << a.get_hour() << ':' << a.get_min() << ':' << s << endl; }
};

extern "C" void CSetHour(HourMin *);     // defined in C
extern "C" void CSetSec(HourMin *);      // defined in C
extern "C" void CSafeSetHour(HourMin *); // defined in C

// wrapper function to be called from C code */
extern "C" void CXXSetHour(HourMin * x) {
  x->set_hour(99);  // much like the C version but the C++
                    // member functions provide some protection
                    // expect 99 % 24, or 3 to be the result
}
// other wrappers may be written to access other member functions
// or operators ...

main() {

  HourMin hm;
  hm.set_hour(18);  // supper time;
  CSetHour(&hm);    // pass address of object to C function
  hm.display();     // hour is out of range

  HourMinSec1 hms1;
  CSetSec((HourMin *) &hms1)
  hms1.display();

  HourMinSec2 hms2;
  CSetSec(&hms2);
  hms2.display();

  CSafeSetHour(&hm); // pass address to a safer C function
  hm.display();     // hour is not out of range
}
图 1。 使用 C++ 源文件 hourclas.cpp 中的定义的 C 源文件 hour.c
/* C code  hour.c */

struct CHourMin {
  int hour;
  int min;
};

void CSetHour(void * v) {
  struct CHourMin * p;
  p = (struct CHourMin *) v; // force it to the type we want
  p->hour = 99;             // with power comes responsibility (oops!)
}

struct CHourMinSec {
  struct CHourMin hourMin;
  int sec;
};

// handles both HourMinSec1, and HourMinSec2 classes

void CSetSec(void *v) {
  struct CHourMinSec * p;
  p = (struct CHourMinSec *) v; // force it to the type we want
  p->sec = 45;
}

void CSafeSetHour(void *v) {
  struct CHourMin * p;
  p = (struct CHourMin *) v; // force it to the type we want

  // ...  do things with p, but be careful
  // ...
  // use a C++ wrapper function to access C++ function members

  CXXSetHour(p);    // almost the same as p->hour = 99
}