wrapper, a C wrapper around a C++ class

TOP | Up | Pheedbak | Download | Tree | Topic | A-Z | Search | Hot | New


If you are creating a new utility, packaged as a library, you may wish to take advantage of modern object oriented techniques and program in C++. But you might be unable to make any assumptions about the programming language used by the calling program. To make your C++ classes accessible to a C language program, you must create a C wrapper around each C++ class.

A C wrapper is nothing but a set of functions, one for each method of the class, compiled (in C++) together with the class. The function that calls the constructor method must return a pointer to the class, and every other function must include the class pointer as one of its arguments.

The header file describing the class and its wrapper should be shared by the class source file and the calling program. The C++ compiler defines (built in) the switch __cplusplus, and the header file can ifdef that switch to present different views to each compiler. The C++ compiler should see the prototypes bracketed by keyword "extern", like this:

		extern "C" { prototype( ); }

but the C compiler should see the prototypes without the keyword "extern" or the curly braces. The C++ compiler should see the class definition as well as the wrapper prototypes, but the C compiler should see only the wrapper prototypes, not the class definition.

This directory contains five files:

It's easy once you know how!


Select any combo of files you'd like to send yourself a compressed tar image of. Executables/scripts are indicated with a trailing `*' character. (depending upon the browser, it may be necessary to hold down the Ctrl key to select/deselect disjoint items.) a compressed tar image of the above-selected items.
OR, ...
a compressed tar image of the entire wrapper directory.

Copyright © 1995, Silicon Graphics, Inc.