ILE C/C++ Programmer's Guide
This program shows how you can access the data members in C++ classes from
source code written in C.
The program consists of these files:
- A C++ source file hourclas.cpp which contains:
- Definitions of one base class, HourMin, and two derived
classes, HourMinSec1 and HourMinSec2
- Three function prototypes with extern "C" linkage:
- extern "C" void CSetHour(HourMin *)
- extern "C" void CSetSec(HourMin *)
- extern "C" void CSafeSetHour(HourMin *)
- The definition of a function with extern "C" linkage,
extern "C" void CXXSetHour(HourMin * x)
- A main() function containing the program logic
See Figure 199.
- A C source file hour.c which contains:
- A structure CHourMin that maps to the C++ class
HourMin in file hourclas.cpp
- Definitions of the three functions with extern "C" linkage
declared in hourclas.cpp
- The definition of a function CSafeSetHour()
Figure 199. C++ Source File hourclas.cpp Definitions Used by C Source File 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
}
|
Figure 200. C Source file hour.c that Uses Definitions from C++ Source File hourclas.cpp
/* 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
}
|
As shown in Figure 199, the main() C++ function:
- Instantiates an object hm of the base class HourMin
- Assigns a value to the h variable (hour) in the base class
- Passes the address of the base class to the function CSetHour()
defined in the C source file hour.c (see Figure 200), which assigns a new value to h in the base
class
- Displays the value of h in the base class
- Instantiates an object hms1 of the derived class
HourMinSec1
- Passes the address of this object class to the function
CSetSec() defined in the C source file hour.c
(see Figure 200), which assigns a value to s in the object
- Displays the value of s in the object
- Instantiates an object hms2 of the class HourMinSec2
which contains the class HourMin
- Passes the address of this new object to the function CSetSec()
defined in the C source file hour.c (see Figure 200), which assigns a value to s in the object
- Displays the value of s in the object
- Passes the address of the base class object to function
SafeSetHour() defined in the C source file hour() which
passes the address back to a function CXXSetHour() defined in the
C++ source file hourclas.cpp (see Figure 199)
The program output is:
+--------------------------------------------------------------------------------+
|99:0 |
|0 -:0 :45 |
|0 -:0 :45 |
|3 -:0 |
|Press ENTER to end terminal session. |
+--------------------------------------------------------------------------------+
[ Top of Page | Previous Page | Next Page | Table of Contents ]
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.