HP aC++/HP C A.06.25 Programmer's Guide
extern “C” void delete_obj (obj_ptr p);
extern “C” void print_obj (obj_ptr p);
struct obj {
private:
int x;
public:
obj() {x = 7;}
friend void print_obj(obj_ptr p);
};
// C interface routine to initialize an
// object by calling the constructor.
void initialize_obj(obj_ptr& p) {
p = new obj;
}
// C interface routine to destroy an
// object by calling the destructor.
void delete_obj(obj_ptr p) {
delete p;
}
// C interface routine to display
// manipulating the object.
void print_obj(obj_ptr p) {
cout << “the value of object->x is “ << p->x << “\n”;
}
Following is a C program that calls the C++ module to manipulate an object:
/***************************************************/
/* C program to demonstrate an interface to the */
/* C++ module. Note that the application needs */
/* to be linked with the aCC driver. */
/***************************************************/
typedef struct obj* obj_ptr;
int main () {
/* C++ object. Notice that none of the
routines should try to manipulate the fields.
*/
obj_ptr f;
/* The first executable statement needs to be a call
to _main so that static objects will be created in
libraries that have constructors defined. In this
application, the stream library contains data
elements that match the conditions.
*/
/* NOTE: In 64-bit mode, you MUST NOT call _main. */
252 Mixing C++ with Other Languages