//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
}
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
#if !defined(__LP64__) && !defined(__ia64) _main();
#endif
/* Initialize the data object. Notice taking the address of f is compatible with the C++ reference construct.
*/
initialize_obj(&f);
/* Call the routine to manipulate the fields */ print_obj(f);
/* Destroy the data object */ delete_obj(f);
}
Compiling and Running the Sample Programs
To compile the example, run the following commands:
cc
aCC
192 Mixing C++ with Other Languages